用python代码ffmpeg获取音视频基本信息,包括视频编码格式、帧率、比特率、分辨率,音频格式、采样率、编码格式、比特率等。
时间: 2023-10-20 14:14:46 浏览: 178
你可以使用Python中的subprocess模块来调用ffmpeg命令,并通过解析输出来获取音视频基本信息。以下是一个示例代码:
```python
import subprocess
def get_media_info(file_path):
command = ['ffmpeg', '-i', file_path]
result = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output = result.stdout.read().decode('utf-8')
lines = output.split('\n')
video_info = {}
audio_info = {}
for line in lines:
if 'Video:' in line:
video_info['codec'] = line.split('Video: ')[1].split(',')[0]
video_info['fps'] = line.split(', ')[3].split(' fps')[0]
video_info['bitrate'] = line.split(', ')[2].split(' kb/s')[0]
video_info['resolution'] = line.split(', ')[2].split('x')[1].split(',')[0]
elif 'Audio:' in line:
audio_info['codec'] = line.split('Audio: ')[1].split(',')[0]
audio_info['samplerate'] = line.split(', ')[2].split(' Hz')[0]
audio_info['bitrate'] = line.split(', ')[3].split(' kb/s')[0]
return video_info, audio_info
file_path = 'example.mp4'
video_info, audio_info = get_media_info(file_path)
print('Video codec:', video_info['codec'])
print('Video fps:', video_info['fps'])
print('Video bitrate:', video_info['bitrate'])
print('Video resolution:', video_info['resolution'])
print('Audio codec:', audio_info['codec'])
print('Audio samplerate:', audio_info['samplerate'])
print('Audio bitrate:', audio_info['bitrate'])
```
在这个示例中,我们使用ffmpeg命令获取媒体文件的信息,并将输出解析为字典。你可以根据需要修改代码以获取其他信息。请确保系统中已安装ffmpeg并将其添加到PATH中。
阅读全文