How to extract duration time from ffmpeg output?

To get a lot of information about the media file, you can do

ffmpeg -i <filename> 

where it will output many lines, in particular

 Duration: 00:08:07.98, start: 0.000000, bitrate: 2080 kb/s 

I would like to output only 00:08:07.98 , so I'm trying

 ffmpeg -i file.mp4 | grep Duration| sed 's/Duration: \(.*\), start/\1/g' 

But he prints everything, not just the length.

Even ffmpeg -i file.mp4 | grep Duration ffmpeg -i file.mp4 | grep Duration prints everything.

How to get the length of the duration?

+47
linux bash ffmpeg
Jun 04 2018-11-11T00:
source share
10 answers

ffmpeg writes this information to stderr , not stdout . Try the following:

 ffmpeg -i file.mp4 2>&1 | grep Duration | sed 's/Duration: \(.*\), start/\1/g' 

Note the redirect from stderr to stdout : 2>&1

EDIT:

Your sed statement doesn't work either. Try the following:

 ffmpeg -i file.mp4 2>&1 | grep Duration | awk '{print $2}' | tr -d , 
+37
Jun 04 2018-11-21T00:
source share
β€” -

You can use ffprobe :

 ffprobe -i <file> -show_entries format=duration -v quiet -of csv="p=0" 

It will display the duration in seconds, for example:

 154.12 

Adding the -sexagesimal option will give the duration as hours: minutes: seconds .microseconds:

 00:02:34.12 
+81
Mar 07 '14 at 7:12
source share

From my experience, many tools offer the desired data in any table / ordering structure, and also offer options for collecting certain parts of this data. This applies, for example, to smartctl, nvidia-smi and ffmpeg / ffprobe. Simply put - often there is no need to collect data around or open subshells for such a task.

As a result, I would use the correct tool for the task - in this case ffprobe will return the value of the raw duration in seconds, after which it would be possible to create the desired time format on my own:

 $ ffmpeg --version ffmpeg version 2.2.3 ... 

The command may vary depending on the version you are using.

 #!/usr/bin/env bash input_file="/path/to/media/file" # Get raw duration value ffprobe -v quiet -print_format compact=print_section=0:nokey=1:escape=csv -show_entries format=duration "$input_file" 

Explanation:

"- v quiet": do not output anything but the desired value of the source data

"- print_format": use a specific format for printing data

"compact =": use compact output format

"print_section = 0": do not print the section name

": nokey = 1": do not print the key pair key: value

": escape = csv": print the value

"- show_entries format = duration": Get field entries with the name duration inside a section called format

Link: ffprobe man pages

+11
Jun 30 '14 at 11:10
source share

In the case of one request parameter, it is easier to use mediainfo and its output formatting as follows (for the duration of the response in milliseconds)

 amber ~ > mediainfo --Output=General;%Duration% ~/work/files/testfiles/+h263_aac.avi 24840 
+5
Jan 29 '13 at 6:43
source share
 ffmpeg -i abc.mp4 2>&1 | grep Duration | cut -d ' ' -f 4 | sed s/,// 

outputs a conclusion

HH: MM: SS.milisecs

+1
Nov 28 '16 at 14:13
source share

For those who want to perform the same calculations without additional software on Windows, enter the script for the command line script:

 set input=video.ts ffmpeg -i "%input%" 2> output.tmp rem search " Duration: HH:MM:SS.mm, start: NNNN.NNNN, bitrate: xxxx kb/s" for /F "tokens=1,2,3,4,5,6 delims=:., " %%i in (output.tmp) do ( if "%%i"=="Duration" call :calcLength %%j %%k %%l %%m ) goto :EOF :calcLength set /A s=%3 set /A s=s+%2*60 set /A s=s+%1*60*60 set /A VIDEO_LENGTH_S = s set /A VIDEO_LENGTH_MS = s*1000 + %4 echo Video duration %1:%2:%3.%4 = %VIDEO_LENGTH_MS%ms = %VIDEO_LENGTH_S%s 

Same answer posted here: How to trim last N seconds from TS video

0
Dec 12 '13 at 22:01
source share

I would just do it in C ++ with a text file and extract the tokens. What for? I am not a Linux terminal expert like others.
To configure it, I would do it on Linux ..

ffmpeg -i 2>&1 | grep "" > mytext.txt

and then run some C ++ application to get the necessary data. Perhaps extract all important values ​​and reformat them for further processing with tokens. I just need to work on my own solution, and people just laugh at me because I'm new to Linux and I don't like too many scripts.

-one
Nov 03 '16 at 2:03
source share

Argh. Forget it. It looks like I should get a web from my C and C ++ programming and use this instead. I don’t know all the shell tricks to make it work. This is how far I got.

ffmpeg -i myfile 2>&1 | grep "" > textdump.txt

and then, most likely, we will select the duration using a C ++ application, extracting tokens.

I am not sending a decision because now I am not a good person.

Update - I have my approach to getting this timestamp

Step 1 - Get Media Information into a Text File
`ffprobe -i myfile 2>&1 | grep "" > textdump.txt`
<B> OR
`ffprobe -i myfile 2>&1 | awk '{ print }' > textdump.txt`

Step 2 - Enter the information you need and extract it
cat textdump.txt | grep "Duration" | awk '{ print $2 }' | ./a.out >
a.out. C, , - - 00: 00: 01.33,
C, stdin . , .

#include stdio.h #include string.h void main() { //by Admiral Smith Nov 3. 2016 char time[80]; int len; char *correct; scanf("%s", &time); correct = (char *)malloc(strlen(time)); if (!correct) { printf("\nmemory error"); return; } memcpy(correct,&time,strlen(time)-1); correct[strlen(time)]='/0'; printf("%s", correct); free(correct); }

Now output formats are correct, like 00: 00: 01.33

-one
Nov 04 '16 at 2:48
source share

You can try the following:

 /* * Determine video duration with ffmpeg * ffmpeg should be installed on your server. */ function mbmGetFLVDuration($file){ //$time = 00:00:00.000 format $time = exec("ffmpeg -i ".$file." 2>&1 | grep 'Duration' | cut -d ' ' -f 4 | sed s/,//"); $duration = explode(":",$time); $duration_in_seconds = $duration[0]*3600 + $duration[1]*60+ round($duration[2]); return $duration_in_seconds; } $duration = mbmGetFLVDuration('/home/username/webdir/video/file.mov'); echo $duration; 
-3
Dec 15 '11 at 23:31
source share

ffmpeg has been replaced by avconv: just replace avconb with Louis Maracio's answer.

 avconv -i file.mp4 2>&1 | grep Duration | sed 's/Duration: \(.*\), start.*/\1/g' 

Note: incremental. * after the start, to get time alone !!

-5
Sep 17 '14 at 4:07
source share



All Articles