How to detect audio sampling rate with avprobe / ffprobe?
I found that the json library can parse the output from ffprobe into a dictionary, and elaborated on your code to store the information inside python. Here's a function that does this and prints the media info if you wish:
import json
from subprocess import check_output
def get_media_info(filename, print_result=True):
"""
Returns:
result = dict with audio info where:
result['format'] contains dict of tags, bit rate etc.
result['streams'] contains a dict per stream with sample rate, channels etc.
"""
result = check_output(['ffprobe',
'-hide_banner', '-loglevel', 'panic',
'-show_format',
'-show_streams',
'-of',
'json', filename])
result = json.loads(result)
if print_result:
print('\nFormat')
for key, value in result['format'].items():
print(' ', key, ':', value)
print('\nStreams')
for stream in result['streams']:
for key, value in stream.items():
print(' ', key, ':', value)
print('\n')
return result
-show_format
shows the container-level information -- i.e. stuff that applies to all streams. Sample rate is a property of a single stream, so it's perfectly normal that -show_format
doesn't display it. You need to use -show_streams
.