Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange
1 of 6

Streamlining @jmunsch's answer, you could end up with something like this:

bc <<<$(for i in *.ts; do LC_ALL=C ffmpeg -i $i 2>&1 | \
        awk -F: '/Duration:/{print $2*3600+$3*60+$4}'; done | tr \\n +)0

Just like he 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 I replace all newlines with plus signs. The expression obtained this way ends in a plus, so I capture the output of all these commands in a big $(…) and append a zero to that. The resulting string gets fed into bc using bash's <<< operator, instead of the more clumsy echo "$(…)0" | bc one might consider instead.