Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get video and audio bitrate of an Mp4 file separately with python?

I have some Mp4 files. I want to get the bitrate of the video track and audio track separately with python. I know that python-opencv may help to get the number of frames:

import cv2
video = "sample.mp4"

video_capture = cv2.VideoCapture(video)
video_length = int(video_capture.get(cv2.CAP_PROP_FRAME_COUNT))

But I didn't find an option for biterate. And I don't need the bitrate overall because I have to process the video and audio separately. (If the audio biterate can be computed by the others, that's alright.) I don't know if python-opencv can make it. If not, what other packages should I use and how?

Use popular packages as much as possible to avoid problems. It's better if stream setting is checked. Thank you.

like image 373
Kani Avatar asked Dec 05 '25 00:12

Kani


1 Answers

ffmpeg is probably the most common or popular solution for video manipulation and you cam use it to get the information you are looking for.

Although ffmpeg is a command line tool, the libraries it is built on can be used in other app and for your case, probably more simply, Python wrappers exist that allow you use all the functionality of command line ffmpeg. For example the following is a well supported one (at the time of writing):

  • https://github.com/kkroening/ffmpeg-python

This library includes examples to get info using ffprobe (companion command line utility to ffmpeg) - the return is JSON and you can search for the info you need in wither the video or audio stream - e.g.:

probe = ffmpeg.probe(args.in_filename)
video_stream = next((stream for stream in probe['streams'] if stream['codec_type'] == 'video'), None)
like image 71
Mick Avatar answered Dec 06 '25 14:12

Mick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!