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?
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 corresponding codec_name
per stream. If you just want the first stream then use V:0
and for audio a:0
.
Chose the output format with the -of
option.
See ffprobe
documentation for more info.