Python从视频(音频/视频)获取流列表

use*_*014 3 python audio video ffmpeg python-3.x

我有一个视频文件,我想从中获取流列表。我可以通过执行一个简单的“ffprobe video.mp4”来查看所需的结果:

....
Stream #0:0(eng): Video: h264 (High) (avc1 / 0x31637661) ......
Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), ......
....
Run Code Online (Sandbox Code Playgroud)

但我需要使用 python 和可以在 Windows 和 Ubuntu 上运行的代码,而无需执行外部进程

我的真正目标是检查视频中是否有任何音频流(简单的是/否就足够了),但我认为获取额外的信息对我的问题有帮助,所以我询问整个流

编辑:澄清我需要避免执行一些外部进程,但寻找一些 python 代码/库在进程内执行它。

Ven*_*cat 6

import os
import json
import subprocess
file_path = os.listdir("path to your videos folder")
audio_flag = False

for file in file_path:
    ffprobe_cmd = "ffprobe -hide_banner -show_streams -print_format json "+file
    process = subprocess.Popen(ffprobe_cmd,stdout=subprocess.PIPE,stderr=subprocess.PIPE, shell=True)
    output = json.loads(process.communicate()[0])

for stream in output["streams"]:
    if(stream['codec_type'] == 'audio'):
        audio_flag = True
        break;
if(audio_flag):
    print("audio present in the file")
else:
    print("audio not present in the file")

# loop through the output streams for more detailed output 
for stream in output["streams"]:
    for k,v in stream.items():
        print(k, ":", v)
Run Code Online (Sandbox Code Playgroud)

注意:确保您的视频文件夹路径仅包含有效的视频文件,因为我在上面的代码片段中没有包含任何文件验证。另外,我还针对包含一个视频流和一个音频流的视频文件测试了此代码。