Home/News | About | Download | Documentation | Forum | Bug Reports | Contact | Donations | Consulting | Projects | Legal | Security | FATE

This forum has not been maintained for a long time and will probably get deleted in the near future.
For faster responses to your questions, please use StackOverflow instead and tag your questions with "FFmpeg".
If you need a backup of the posts from this forum, please contact me directly.

Creating a listing of video files with their run times

A collection of useful tutorials for some common tasks.

Creating a listing of video files with their run times

Postby pmennen » Mon Oct 12, 2015 1:30 pm

Often I found it useful to create a list of all the video files in a folder, preceded by the video runtime for each file.
I also thought it would be nice to give a total runtime for all the files at the end.
So for example, if a folder contained three video files, a directory listing like this would be created:

01:40:43.66 --- Encounters at the End of the World.mp4
00:39:05.35 --- Antarctica (IMAX).avi
01:10:15.91 --- The Great White Silence (Captain Scott) 1924.avi
003:30:03 ==================== Total run time (3 video files)

I created a batch file (which calls ffmpeg of course) which does exactly this. I have attached it to the end of this post in the hopes that some of you may also find it useful.
It was harder than I thought to get it to work properly, which may indicate that a batch file was not the best approach to the problem. Never the less, that's they way I started.
I also have a non-recursive version (i.e. skipping the subfolders) which is essentially the same except that the /R switch is removed from the for loop.

By the way, I ignored the tenths and hundredths of seconds when computing the total, so don't expect the seconds field of the result to be perfect. (I didn't think it was worth the trouble to go for that level of hyper accuracy).

Perhaps some of you may offer some alternative approaches or suggestions. Although I'm mostly satisfied with the output, there are a few minor annoyances.
The most significant annoyance is that all the mp4 files are listed first (alphabetically) followed by the avi files, then mkv and finally flv files.
I think it would be nicer to list all the files alphabetically without regard to the extension. I don't know if the for loop can be rearranged to make that happen, or perhaps the fix would be to make a final pass thru "sort" before appending the total run time at the end.

Another minor annoyance is that only the file name (without the path) appears on the listing. At least for the recursive version, perhaps it would be more useful to have the full file name (i.e. including the path) shown in the listing. Perhaps that is not difficult, although my first casual attempt at making it work that way failed.

I also noticed that if there was an "&" (ampersand) character anywhere in the file name, the output for that line would get a little funky. Not sure exactly why, although I rarely use that character in a file name, so I didn't worry too much about that one.

BTW, this was done under windows 10 using gnu sed, although I suspect it would work even with windows versions as old as XP.

I welcome any comments or criticism :)

Code: Select all

@echo off

REM - Clear out vidtime.txt file in case it already exists
type NUL > vidtime.txt

REM - Run ffmpeg for every video file in the current folder
REM - Save only the characters following "Duration:" up to the first comma
for /R %%G IN (*.mp4 *.avi *.mkv *.flv) DO (
  ffmpeg -i "%%G" 2>&1 | sed -n "s_ Duration: \([^,]*\).*_\1 --- %%~nxG_p" >> vidtime.txt )

REM - The fi variable will count the number of files processed
REM - The sec variable will total running time in seconds
REM - We have to handle each digit of the time separately (otherwise we get the leading zero octal problem)
set sec=0 & set fi=0
sed "s_0\(.\):\(.\)\(.\):\(.\)\(.\).*_set /A fi+=1 \& set /A sec+=\1*3600+\2*600+\3*60+\4*10+\5_" vidtime.txt > sec.bat

REM - Execute (and delete) the sec batch file to count the files and sum up the durations
call sec
del sec.bat

REM - Convert seconds to hours/minutes/seconds
REM - Zeropad minutes and seconds by adding 100 and then removing the first digit
REM - Zeropad hours by adding 1000 and then removing the first digit
set /A min = sec/60
set /A sec += 100 - 60*min
set /A hour = min/60
set /A min += 100 - 60*hour
set /A hour += 1000

REM - Append the running time and file count to the end of the results file (vidtime.txt)
echo %hour:~1%:%min:~1%:%sec:~1% ==================== Total run time (%fi% video files) >> vidtime.txt

REM - Display the results and delete the sec batch
type vidtime.txt

Posts: 1
Joined: Mon Oct 12, 2015 11:58 am

Re: Creating a listing of video files with their run times

Postby llogan » Mon Oct 12, 2015 6:04 pm

Note that duration can be considered fragile; especially if the file is truncated or damaged. If you find that the durations are incorrect then you can completely decode the input:
Code: Select all
ffmpeg -i input -f null -

...and refer to "time=" near the bottom of the console output.

Also, ffprobe may provide an easier method to get duration since it can output just the duration, or whatever or parameter(s) you need. However, the note about duration still applies.
Get an answer more quickly by providing your actual ffmpeg command and the complete console output. Use the code button to format your command and output or your question will be ignored.
Posts: 2363
Joined: Fri Jan 25, 2013 9:47 pm
Location: Alaska

Return to Tutorials

Who is online

Users browsing this forum: No registered users and 2 guests