Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop Over Video Files in Folder to get video length

Tags:

applescript

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
like image 691
Jon Avatar asked Oct 21 '11 20:10

Jon


People also ask

How do I determine the length of a video folder?

Select all files in question inside File Explorer. Take a Right Click & Go to Properties. In Properties Window, Take Details Tab. Lookout for the value of Length and there it is!

How do I find the duration of multiple video clips on a Mac?

The easiest answer is to highlight each video and hit Command-I for "Get Info" and the window will show the duration. Add them up for total duration. You can also use iMovie and QuickTime Player to do this.


1 Answers

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
like image 161
regulus6633 Avatar answered Oct 18 '22 02:10

regulus6633