I am making a python script that uses ffmpeg and moviepy to convert videos to mp4. I want to make an if statement that checks if the input file needs to be reencoded or just rewrapped.(if the input file is aac and h.264 it does not need to be reencoded.) Is there a simple way I can grab that file info?
2 Answers
I think the easiest way to do this is to use ffprobe
, and have it output JSON data which you can parse within your application.
Try something like this:
ffprobe -v quiet -print_format json -show_format -show_streams input.mp4
The JSON will come out STDOUT, so if you execute this as a child process, you just need to be sure to catch its output in your application, and then parse.

- 159,648
- 54
- 349
- 530
Use ffprobe
. Example JSON output:
$ ffprobe -loglevel error -show_entries stream=codec_name -of json input.mkv
{
"programs": [
],
"streams": [
{
"codec_name": "h264"
},
{
"codec_name": "aac"
}
]
}
Examples showing video and audio separately (-select_streams
) and only output the codec_name
value:
$ ffprobe -loglevel error -select_streams V -show_entries stream=codec_name -of csv=p=0 input.mkv
h264
$ ffprobe -loglevel error -select_streams a -show_entries stream=codec_name -of csv=p=0 input.mkv
aac
-select_streams V
will choose all non-image video streams in the input. So if you have more than one video stream it will output each correspondingcodec_name
per stream. If you just want the first stream then useV:0
and for audioa:0
.Chose the output format with the
-of
option.See
ffprobe
documentation for more info.

- 121,796
- 28
- 232
- 243