循环文件夹中的视频文件以获取视频长度

发布于 2024-12-11 16:26:29 字数 375 浏览 0 评论 0原文

我有以下内容返回所选视频文件的秒数。

然而,我正在寻找一种方法,只给它电影文件夹,然后让它循环遍历所有子目录并查找所有视频文件类型。

一旦有了这些,我想以“1小时53秒”类型格式列出视频长度,因为“7990秒”并没有太大帮助。

谢谢

set macPath to (choose file) as text
tell application "System Events"
    set ts to time scale of movie file macPath
    set dur to duration of movie file macPath
    set movieTime to dur / ts
end tell

I have the following which returns how many seconds a selected video file is.

However I was after a way to just give it the movie folder and for it to then loop through all subdirectories and find all video file types.

Once it has these I would like to list the video length in "1 hour 53 seconds" type format as "7990 seconds" isn't too helpful.

Thanks

set macPath to (choose file) as text
tell application "System Events"
    set ts to time scale of movie file macPath
    set dur to duration of movie file macPath
    set movieTime to dur / ts
end tell

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧人哭 2024-12-18 16:26:29

您的问题涉及几个子问题。

1)如何获取文件夹中的所有文件,包括子文件夹
2)如何过滤该列表以仅包含视频文件
3)如何循环浏览视频文件列表并从每个文件中提取信息
4)如何将秒转换为可用的字符串

通常我会要求您将其分解为单独的问题,因为对于某人来说为您写下整个内容是一项艰巨的任务。然而,在这种情况下,你很幸运,因为我之前就做过这件事......所以你可以得到我的脚本。我在代码中添加了很多注释来帮助您了解它是如何工作的。

-- I found these extensions for video files here http://www.fileinfo.net/filetypes/video
-- we can check the file extensions of a file against this list to evaluate if it's a video file
set video_ext_list to {"3g2", "3gp", "3gp2", "3gpp", "3mm", "60d", "aep", "ajp", "amv", "asf", "asx", "avb", "avi", "avs", "bik", "bix", "box", "byu", "cvc", "dce", "dif", "dir", "divx", "dv", "dvr-ms", "dxr", "eye", "fcp", "flc", "fli", "flv", "flx", "gl", "grasp", "gvi", "gvp", "ifo", "imovieproject", "ivf", "ivs", "izz", "izzy", "lsf", "lsx", "m1v", "m2v", "m4e", "m4u", "m4v", "mjp", "mkv", "moov", "mov", "movie", "mp4", "mpe", "mpeg", "mpg", "mpv2", "msh", "mswmm", "mvb", "mvc", "nvc", "ogm", "omf", "prproj", "prx", "qt", "qtch", "rm", "rmvb", "rp", "rts", "sbk", "scm", "smil", "smv", "spl", "srt", "ssm", "svi", "swf", "swi", "tivo", "ts", "vdo", "vf", "vfw", "vid", "viewlet", "viv", "vivo", "vob", "vro", "wm", "wmd", "wmv", "wmx", "wvx", "yuv"}

-- get the folder to check
set f to choose folder

-- notice the use of "entire contents" to also go through subfolders of f
-- use a "whose" filter to find only the video files
tell application "Finder"
    set vidFiles to (files of entire contents of f whose name extension is in video_ext_list) as alias list
end tell

-- use a repeat loop to loop over a list of something
set vidList to {} -- this is where we store the information as we loop over the files
repeat with aFile in vidFiles
    -- get some information from aFile
    tell application "System Events"
        set vidFile to movie file (aFile as text)
        set ts to time scale of vidFile
        set dur to duration of vidFile
    end tell

    -- add the information to the "storage" list we made earlier
    set end of vidList to {POSIX path of aFile, secs_to_hms(dur / ts)}
end repeat

return vidList

(*=================== SUBROUTINES ===================*)
-- convert seconds into a string of words
-- the use of "mod" and "div" here makes it easy
-- we also make sure that each value is at least 2 places long to make it look nicer
on secs_to_hms(the_secs)
    set timeString to ""
    set hr to the_secs div hours
    if hr is not 0 then set timeString to timeString & (text -2 thru -1 of ("0" & (hr as text))) & " hours "

    set min to the_secs mod hours div minutes
    if min is not 0 then set timeString to timeString & (text -2 thru -1 of ("0" & (min as text))) & " minutes "

    set sec to the_secs mod minutes div 1
    if sec is not 0 then
        set fraction to text 2 thru 3 of ((100 + the_secs mod 1 * 100) as text)
        set timeString to timeString & (sec as text) & "." & fraction & " seconds"
    end if

    if timeString ends with space then set timeString to text 1 thru -2 of timeString
    return timeString
end secs_to_hms

You have several sub-questions involved in your question.

1) How do I get all of the files in a folder, including the sub folders
2) how do I filter that list to only include video files
3) How do I loop through that list of video files and extract information from each and
4) How do I convert seconds into a useable string of words

Normally I would ask that you break it down into those individual questions because it's a large task for someone to write the whole thing for you. However, in this case you're lucky because I had done this before myself... so you can have my script. I put lots of comments in the code to help you learn how it works.

-- I found these extensions for video files here http://www.fileinfo.net/filetypes/video
-- we can check the file extensions of a file against this list to evaluate if it's a video file
set video_ext_list to {"3g2", "3gp", "3gp2", "3gpp", "3mm", "60d", "aep", "ajp", "amv", "asf", "asx", "avb", "avi", "avs", "bik", "bix", "box", "byu", "cvc", "dce", "dif", "dir", "divx", "dv", "dvr-ms", "dxr", "eye", "fcp", "flc", "fli", "flv", "flx", "gl", "grasp", "gvi", "gvp", "ifo", "imovieproject", "ivf", "ivs", "izz", "izzy", "lsf", "lsx", "m1v", "m2v", "m4e", "m4u", "m4v", "mjp", "mkv", "moov", "mov", "movie", "mp4", "mpe", "mpeg", "mpg", "mpv2", "msh", "mswmm", "mvb", "mvc", "nvc", "ogm", "omf", "prproj", "prx", "qt", "qtch", "rm", "rmvb", "rp", "rts", "sbk", "scm", "smil", "smv", "spl", "srt", "ssm", "svi", "swf", "swi", "tivo", "ts", "vdo", "vf", "vfw", "vid", "viewlet", "viv", "vivo", "vob", "vro", "wm", "wmd", "wmv", "wmx", "wvx", "yuv"}

-- get the folder to check
set f to choose folder

-- notice the use of "entire contents" to also go through subfolders of f
-- use a "whose" filter to find only the video files
tell application "Finder"
    set vidFiles to (files of entire contents of f whose name extension is in video_ext_list) as alias list
end tell

-- use a repeat loop to loop over a list of something
set vidList to {} -- this is where we store the information as we loop over the files
repeat with aFile in vidFiles
    -- get some information from aFile
    tell application "System Events"
        set vidFile to movie file (aFile as text)
        set ts to time scale of vidFile
        set dur to duration of vidFile
    end tell

    -- add the information to the "storage" list we made earlier
    set end of vidList to {POSIX path of aFile, secs_to_hms(dur / ts)}
end repeat

return vidList

(*=================== SUBROUTINES ===================*)
-- convert seconds into a string of words
-- the use of "mod" and "div" here makes it easy
-- we also make sure that each value is at least 2 places long to make it look nicer
on secs_to_hms(the_secs)
    set timeString to ""
    set hr to the_secs div hours
    if hr is not 0 then set timeString to timeString & (text -2 thru -1 of ("0" & (hr as text))) & " hours "

    set min to the_secs mod hours div minutes
    if min is not 0 then set timeString to timeString & (text -2 thru -1 of ("0" & (min as text))) & " minutes "

    set sec to the_secs mod minutes div 1
    if sec is not 0 then
        set fraction to text 2 thru 3 of ((100 + the_secs mod 1 * 100) as text)
        set timeString to timeString & (sec as text) & "." & fraction & " seconds"
    end if

    if timeString ends with space then set timeString to text 1 thru -2 of timeString
    return timeString
end secs_to_hms
沉溺在你眼里的海 2024-12-18 16:26:29

我看到这篇文章是因为我想在文件夹中保存视频文件的日志;我可以在电子表格中导入一些东西,也可以计算总持续时间,但不仅仅是。

发布的脚本对我不起作用,所以我最终在 Final Cut Pro 中导入文件夹,对文件夹进行批量导出,然后选择“文件”>“文件”。出口>批次列表,生成一个纯文本文件,我可以将其导入到电子表格中作为日志的开头并计算总持续时间。

也许这对其他人有帮助。

I came across this post because I wanted to have a log of video files in a folder; something I could import in a spreadsheet, also to calculate the total duration but not only.

The posted script didn't work for me so I ended up importing the folder in Final Cut Pro, doing Batch Export on the folder and than File > Export > Batch List, which resulted in a plain text file I could import in a spreadsheet as the start of a log and to calculate the total duration.

Perhaps this helps others.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文