How to extract duration time from ffmpeg output?
To get a lot of information about a media file one can do
ffmpeg -i <filename>
where it will output a lot of lines, one 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 try
ffmpeg -i file.mp4 | grep Duration| sed 's/Duration: \(.*\), start/\1/g'
But it prints everything, and not just the length.
Even ffmpeg -i file.mp4 | grep Duration
outputs everything.
How do I get just the duration length?
Solution 1:
You can use ffprobe
:
ffprobe -i <file> -show_entries format=duration -v quiet -of csv="p=0"
It will output the duration in seconds, such as:
154.12
Adding the -sexagesimal
option will output duration as hours:minutes:seconds.microseconds:
00:02:34.12
Solution 2:
ffmpeg is writing that information to stderr
, not stdout
. Try this:
ffmpeg -i file.mp4 2>&1 | grep Duration | sed 's/Duration: \(.*\), start/\1/g'
Notice the redirection of stderr
to stdout
: 2>&1
EDIT:
Your sed
statement isn't working either. Try this:
ffmpeg -i file.mp4 2>&1 | grep Duration | awk '{print $2}' | tr -d ,
Solution 3:
From my experience many tools offer the desired data in some kind of a table/ordered structure and also offer parameters to gather specific parts of that data. This applies to e.g. smartctl, nvidia-smi and ffmpeg/ffprobe, too. Simply speaking - often there's no need to pipe data around or to open subshells for such a task.
As a consequence I'd use the right tool for the job - in that case ffprobe would return the raw duration value in seconds, afterwards one could create the desired time format on his own:
$ ffmpeg --version
ffmpeg version 2.2.3 ...
The command may vary dependent 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"
An explanation:
"-v quiet": Don't output anything else but the desired raw data value
"-print_format": Use a certain format to print out the data
"compact=": Use a compact output format
"print_section=0": Do not print the section name
":nokey=1": do not print the key of the key:value pair
":escape=csv": escape the value
"-show_entries format=duration": Get entries of a field named duration inside a section named format
Reference: ffprobe man pages
Solution 4:
I recommend using json format, it's easier for parsing
ffprobe -i your-input-file.mp4 -v quiet -print_format json -show_format -show_streams -hide_banner
{
"streams": [
{
"index": 0,
"codec_name": "aac",
"codec_long_name": "AAC (Advanced Audio Coding)",
"profile": "HE-AACv2",
"codec_type": "audio",
"codec_time_base": "1/44100",
"codec_tag_string": "[0][0][0][0]",
"codec_tag": "0x0000",
"sample_fmt": "fltp",
"sample_rate": "44100",
"channels": 2,
"channel_layout": "stereo",
"bits_per_sample": 0,
"r_frame_rate": "0/0",
"avg_frame_rate": "0/0",
"time_base": "1/28224000",
"duration_ts": 305349201,
"duration": "10.818778",
"bit_rate": "27734",
"disposition": {
"default": 0,
"dub": 0,
"original": 0,
"comment": 0,
"lyrics": 0,
"karaoke": 0,
"forced": 0,
"hearing_impaired": 0,
"visual_impaired": 0,
"clean_effects": 0,
"attached_pic": 0
}
}
],
"format": {
"filename": "your-input-file.mp4",
"nb_streams": 1,
"nb_programs": 0,
"format_name": "aac",
"format_long_name": "raw ADTS AAC (Advanced Audio Coding)",
"duration": "10.818778",
"size": "37506",
"bit_rate": "27734",
"probe_score": 51
}
}
you can find the duration information in format section, works both for video and audio