python 查看视频文件 音频轨道长度和视频轨道长度
时间: 2023-11-27 11:55:12 浏览: 150
你可以使用FFmpeg库来查看视频文件的音频轨道长度和视频轨道长度。以下是一个示例代码:
```python
import subprocess
def get_video_info(video_path):
cmd = ['ffprobe', '-v', 'error', '-select_streams', 'v:0', '-show_entries', 'stream=width,height,duration', '-of', 'default=noprint_wrappers=1:nokey=1', video_path]
output = subprocess.check_output(cmd, stderr=subprocess.STDOUT).decode('utf-8')
width, height, duration = output.strip().split('\n')
return int(width), int(height), float(duration)
def get_audio_info(video_path):
cmd = ['ffprobe', '-v', 'error', '-select_streams', 'a:0', '-show_entries', 'stream=duration', '-of', 'default=noprint_wrappers=1:nokey=1', video_path]
output = subprocess.check_output(cmd, stderr=subprocess.STDOUT).decode('utf-8')
duration = output.strip()
return float(duration)
video_path = '/path/to/video.mp4'
width, height, video_duration = get_video_info(video_path)
audio_duration = get_audio_info(video_path)
print(f'Video width: {width}')
print(f'Video height: {height}')
print(f'Video duration: {video_duration}')
print(f'Audio duration: {audio_duration}')
```
这个代码使用了 `ffprobe` 命令行工具来获取视频信息。`get_video_info` 函数用来获取视频的宽度、高度和时长,而 `get_audio_info` 函数则用来获取音频轨道的时长。你可以将视频文件路径传递给这两个函数,并将返回的值打印出来。
阅读全文