132

Every time I try to get some information about my video files with ffmpeg, it pukes a lot of useless information mixed with good things.

I'm using ffmpeg -i name_of_the_video.mpg.

There are any possibilities to get that in a friendly way? I mean JSON would be great (and even ugly XML is fine).

By now, I made my application parse the data with regex but there are lots of nasty corners that appear on some specific video files. I fixed all that I encountered, but there may be more.

I wanted something like:

{
  "Stream 0": {
     "type": "Video",
     "codec": "h264",
     "resolution": "720x480"
  },
  "Stream 1": {
     "type": "Audio",
     "bitrate": "128 kbps",
     "channels": 2
  }
}
JBernardo
  • 32,262
  • 10
  • 90
  • 115

4 Answers4

318

A bit late, but perhaps still relevant to someone..

ffprobe is indeed an excellent way to go. Note, though, that you need to tell ffprobe what information you want it to display (with the -show_format, -show_packets and -show_streams options) or it'll just give you blank output (like you mention in one of your comments).

For example, ffprobe -v quiet -print_format json -show_format -show_streams somefile.asf would yield something like the following:

{
  "streams": [{
    "index": 0,
    "codec_name": "wmv3",
    "codec_long_name": "Windows Media Video 9",
    "codec_type": "video",
    "codec_time_base": "1/1000",
    "codec_tag_string": "WMV3",
    "codec_tag": "0x33564d57",
    "width": 320,
    "height": 240,
    "has_b_frames": 0,
    "pix_fmt": "yuv420p",
    "level": -99,
    "r_frame_rate": "30000/1001",
    "avg_frame_rate": "0/0",
    "time_base": "1/1000",
    "start_time": "0.000",
    "duration": "300.066",
    "tags": {
        "language": "eng"
    }
  }],
  "format": {
    "filename": "somefile.asf",
    "nb_streams": 1,
    "format_name": "asf",
    "format_long_name": "ASF format",
    "start_time": "0.000",
    "duration": "300.066",
    "tags": {
        "WMFSDKVersion": "10.00.00.3646",
        "WMFSDKNeeded": "0.0.0.0000",
        "IsVBR": "0"
    }
  }
}
Irexistus
  • 3,194
  • 1
  • 13
  • 8
  • 3
    If anyone ever need, I've written a PHP solution based on that answer: http://stackoverflow.com/questions/11805207/how-to-parse-ffmpeg-info-in-php#11807265 – Paulo Freitas Aug 05 '12 at 11:33
  • 1
    Sorry for digging this out, but is there any chance to speed this up? It takes 5-6 seconds to show the output – mangia Aug 30 '14 at 21:45
  • That final sentence is exactly what I needed. Thanks :) – Matt Fletcher Nov 05 '14 at 16:58
  • my app close with out displaying any message, when I use ffprob command! why??? but ffmpeg works fine – farhad.kargaran Sep 03 '15 at 04:23
  • 6
    people... asking a question in a comment to an answer from 4 years ago has about zero chance of getting answered... – patrick Sep 04 '15 at 19:06
  • Other formats can also be used - XML etc: https://ffmpeg.org/ffprobe-all.html#toc-Writers – Wilf Mar 21 '16 at 23:39
  • 3
    You can also use ```-of json``` instead of ```-print_format json```. The former is compatible with ```avprobe``` while the latter not. – Chih-Hsuan Yen Jul 08 '16 at 13:50
  • I find myself coming back to this somewhat often. Great answer! – Salem May 28 '17 at 12:29
  • This is the correct answer! I was wondering why `ffmpeg` cannot output the same results, considering that `ffprobe` is built using the same libraries... – loretoparisi Oct 19 '22 at 10:04
  • @patrick I would more blame SE's habit of killing new discussion than the people who are trying to make discussion. – Rei Miyasaka Aug 07 '23 at 00:13
  • @ReiMiyasaka, I guess you're right considering this question is from 2011, my answer from 2015 and here we are talking about is 7 years later – patrick Aug 24 '23 at 15:41
22

Now is possible to use -progress - to print friendly info formatted by key=value.

ffmpeg  -i video.mp4 .......-s 1920x1080 -progress - -y out.mp4

speed=5.75x
frame=697
fps=167.7
stream_0_0_q=39.0
bitrate=2337.0kbits/s
total_size=6979778
out_time_ms=23893333
out_time=00:00:23.893333
dup_frames=0
drop_frames=0
Dmitriy
  • 5,525
  • 12
  • 25
  • 38
Oleksandr Kyrpa
  • 638
  • 10
  • 17
13

You could try ffprobe. The correct command to get JSON output should look like the following:

ffprobe ... -print_format json
NT3RP
  • 15,262
  • 9
  • 61
  • 97
  • 2
    I compiled the newest ffmpeg but the json output of the program is only `{}`. It doesn't seems to work with the video files I have... – JBernardo Oct 11 '11 at 02:44
  • 3
    @JBernardo Hey, in case anyone else runs into this, you have to do -print_format rather then -of or else it will just do a very unhelpful { ... normal print output here ... }, see the accepted answer for an example command line – mgrandi Jul 01 '14 at 02:31
4

Another usage of ffprobe which is nicely parseable:

ffprobe -v error -select_streams v:0 -show_entries stream=width,height,r_frame_rate,bit_rate,codec_name,duration -of csv=p=0:s=x video.mp4

results in:

h264x600x480x25/1x385.680000x542326

-select_streams v:0 selects only the first video stream. If you remove that parameter you get one line for each stream.

yglodt
  • 13,807
  • 14
  • 91
  • 127
  • The `s=x` sets the character that separates the fields when using `csv` output format. Useful for [asking for `width,height` to get output like `1280x720`](https://stackoverflow.com/a/29585066/). Pretty ugly otherwise. Default is a comma if you remove `:s=x`, or you can always use a different [`-of` type](https://ffmpeg.org/ffprobe.html#Writers) such as json. – llogan Dec 17 '20 at 18:09