I have a list of .ts files:
out1.ts ... out749.ts out8159.ts out8818.ts
How can I get the total duration (running time) of all these files?
I have a list of .ts files:
out1.ts ... out749.ts out8159.ts out8818.ts
How can I get the total duration (running time) of all these files?
I have no .ts here (only .mp4) but this should work for all video files:
Use ffprobe (part of ffmpeg) to get the time in seconds, e.g:
ffprobe -v quiet -of csv=p=0 -show_entries format=duration Inception.mp4
275.690000
So for all video files you could use a for loop and awk to calculate the total time in seconds:
for f in ./*.mp4
do ffprobe -v quiet -of csv=p=0 -show_entries format=duration "$f"
done | awk '{sum += $1}; END{print sum}'
2735.38
To further process the output to convert the total to DD:HH:MM:SS, see the answers here.
Another way is via exiftool which has an internal ConvertDuration:
exiftool -n -q -p '${Duration;our $sum;$_=ConvertDuration($sum+=$_)
}' ./*.mp4 | tail -n1
0:45:35
This uses ffmpeg and prints the time out in total seconds:
times=()
for f in *.ts; do
_t=$(ffmpeg -i "$f" 2>&1 | grep "Duration" | grep -o " [0-9:.]*, " | head -n1 | tr ',' ' ' | awk -F: '{ print ($1 * 3600) + ($2 * 60) + $3 }')
times+=("$_t")
done
echo "${times[@]}" | sed 's/ /+/g' | bc
Explanation:
for f in *.ts; do iterates each of the files that ends in ".ts"
ffmpeg -i "$f" 2>&1 redirects output to stderr
grep "Duration" | grep -o " [0-9:.]*, " | head -n1 | tr ',' ' ' isolates the time
awk -F: '{ print ($1 * 3600) + ($2 * 60) + $3 }' Converts time to seconds
times+=("$_t") adds the seconds to an array
echo "${times[@]}" | sed 's/ /+/g' | bc expands each of the arguments and replaces the spaces and pipes it to bc a common linux calculator
Streamlining @jmunsch's answer, and using the paste I just learned from @slm's answer, you could end up with something like this:
for i in *.ts; do LC_ALL=C ffmpeg -i "$i" 2>&1 | \
awk -F: '/Duration:/{print $2*3600+$3*60+$4}'; done | paste -sd+ | bc
Just like jmunsch did, I'm using ffmpeg to print the duration, ignoring the error about a missing output file and instead searching the error output for the duration line. I invoke ffmpeg with all aspects of the locale forced to the standard C locale, so that I won't have to worry about localized output messages.
Next I'm using a single awk instead of his grep | grep | head | tr | awk. That awk invocation looks for the (hopefully unique) line containing Duration:. Using colon as a separator, that label is field 1, the hours are field 2, the minutes filed 3 and the seconds field 4. The trailing comma after the seconds doesn't seem to bother my awk, but if someone has problems there, he could include a tr -d , in the pipeline between ffmpeg and awk.
Now comes the part from slm: I'm using paste to replace newlines with plus signs, but without affecting the trailing newline (contrary to the tr \\n + I had in a previous version of this answer). That gives the sum expression which can be fed to bc.
Inspired by slm's idea of using date to handle time-like formats, here is a version which does use it to format the resulting seconds as days, hours, minutes and seconds with fractional part:
TZ=UTC+0 date +'%j %T.%N' --date=@$(for i in *.ts; do LC_ALL=C \
ffmpeg -i "$i" 2>&1 | awk -F: '/Duration:/{print $2*3600+$3*60+$4}'; done \
| paste -sd+ | bc) | awk '{print $1-1 "d",$2}' | sed 's/[.0]*$//'
The part inside $(…) is exactly as before. Using the @ character as an indication, we use this as the number of seconds since the 1 January 1970. The resulting “date” gets formatted as day of the year, time and nanoseconds. From that day of the year we subtract one, since an input of zero seconds already leads to day 1 of that year 1970. I don't think there is a way to get day of the year counts starting at zero.
The final sed gets rid of extra trailing zeros. The TZ setting should hopefully force the use of UTC, so that daylight saving time won't interfere with really large video collections. If you have more than one year worth of video, this approach still won't work, though.
I'm not familiar with the .ts extension, but assuming they're some type of video file you can use ffmpeg to identify the duration of a file like so:
$ ffmpeg -i some.mp4 2>&1 | grep Dura
Duration: 00:23:17.01, start: 0.000000, bitrate: 504 kb/s
We can then split this output up, selecting just the duration time.
$ ffmpeg -i some.mp4 2>&1 | grep -oP "(?<=Duration: ).*(?=, start.*)"
00:23:17.01
So now we just need a way to iterate through our files and collect these duration values.
$ for i in *.mp4; do
ffmpeg -i "$i" 2>&1 | grep -oP "(?<=Duration: ).*(?=, start.*)"; done
00:23:17.01
00:23:17.01
00:23:17.01
NOTE: Here for my example I simply copied my sample file some.mp4 and named it 1.mp4, 2.mp4, and 3.mp4.
The following snippet will take the durations from above and convert them to seconds.
$ for i in *.mp4; do
dur=$(ffmpeg -i "$i" 2>&1 | grep -oP "(?<=Duration: ).*(?=, start.*)");
date -ud "1970/01/01 $dur" +%s; done
1397
1397
1397
This takes our durations and puts them in a variable, $dur, as we loop through the files. The date command is then used to calculate the number of seconds sine the Unix epoch (1970/01/01). Here's the above date command broken out so it's easier to see:
$ date -ud "1970/01/01 00:23:17.01" +%s
1397
NOTE: Using date in this manner will only work if all your files have a duration that's < 24 hours (i.e. 86400 seconds). If you need something that can handle larger durations you can use this as an alternative:
sed 's/^/((/; s/:/)*60+/g' | bc
Example
$ echo 44:29:36.01 | sed 's/^/((/; s/:/)*60+/g' | bc
160176.01
We can then take the output of our for loop and run it into a paste command which will incorporate + signs in between each number, like so:
$ for i in *.mp4; do
dur=$(ffmpeg -i "$i" 2>&1 | grep -oP "(?<=Duration: ).*(?=, start.*)");
date -ud "1970/01/01 $dur" +%s; done | paste -s -d+
1397+1397+1397
Finally we run this into the command line calculator, bc to sum them up:
$ for i in *.mp4; do
dur=$(ffmpeg -i "$i" 2>&1 | grep -oP "(?<=Duration: ).*(?=, start.*)");
date -ud "1970/01/01 $dur" +%s; done | paste -s -d+ | bc
4191
Resulting in the total duration of all the files, in seconds. This can of course be converted to some other format if needed.
Well, these all solution need a bit of work, what I did was very simple, 1)
went to the desired folder and right click -> open with other application
Then select VLC media player,
this will start playing one of the videos, but then
press ctrl + L, and you will see the playlist of the videos and somewhere on top left corner you will see total duration
here is example
You can see just under the toolbar, there is Playlist[10:35:51] written, so folder contains 10 hours 35 mins and 51 sec duration of total videos
Going off the accepted answer and using the classic UNIX reverse-polish tool:
{ find . -maxdepth 2 -iname '*.mp4' -exec ffprobe -v quiet -of csv=p=0 \
-show_entries format=duration {} \; ; printf '+\n60\n*\np'; } | dc
783.493000
I.e.: Appening + and p then piping that into dc and you'll get your sum.
$ find -iname '*.ts' -print0 |\
xargs -0 mplayer -vo dummy -ao dummy -identify 2>/dev/null |\
perl -nle '/ID_LENGTH=([0-9\.]+)/ && ($t += $1) && printf "%02d:%02d:%02d:%02d\n",$t/86400,$t/3600%24,$t/60%60,$t%60'
Be sure that you have MPlayer installed.
I had sub-directories on the current folder so I had to recursively compute the duration:
find . -iname '*.mp4' -print0 | xargs --null exiftool -n -q -p '${Duration;our $sum;$_=ConvertDuration($sum+=$_)}' | tail -n1