Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python从视频(音频/视频)获取流列表_Python_Python 3.x_Audio_Video_Ffmpeg - Fatal编程技术网

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

Python从视频(音频/视频)获取流列表,python,python-3.x,audio,video,ffmpeg,Python,Python 3.x,Audio,Video,Ffmpeg,我有一个视频文件,我想从中获取流列表。我可以通过执行一个简单的`ffprobe video.mp4: .... Stream #0:0(eng): Video: h264 (High) (avc1 / 0x31637661) ...... Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), ...... .... 但是我需要使用python和可以在Windows和Ubuntu上运行的代码,而无需执行外部进程 我真正的目标是检查视频中是否

我有一个视频文件,我想从中获取流列表。我可以通过执行一个简单的`ffprobe video.mp4:

....
Stream #0:0(eng): Video: h264 (High) (avc1 / 0x31637661) ......
Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), ......
....
但是我需要使用python和可以在Windows和Ubuntu上运行的代码,而无需执行外部进程

我真正的目标是检查视频中是否有任何音频流(简单的是/否就足够了),但我认为获取额外的信息可能会有助于解决我的问题,因此我询问的是整个音频流

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

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)

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

我看到了它,但我需要从python中执行它