简体   繁体   English

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

[英]Python get list of streams from video (audio/video)

I have a video file and I want to get the list of streams from it. 我有一个视频文件,我想从中获取流的列表。 I can see the needed result by for example executing a simple `ffprobe video.mp4: 我可以通过执行一个简单的`ffprobe video.mp4来查看所需的结果:

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

But I need to use python and code that will work both on Windows and Ubuntu, without executing an external process . 但是我需要使用在Windows和Ubuntu上都可以运行的python和代码, 而无需执行外部进程

My real goal is to check whether there is ANY audio stream within the video (a simple yes/no would suffice), but I think getting extra information can be helpful for my problem, so I'm asking about the entire streams 我的真正目标是检查视频中是否有任何音频流(简单的是/否就足够了),但是我认为获取更多信息可以解决我的问题,因此我要询问整个流

EDIT: Clarifying that I need to avoid executing some external process, but looking for some python code/library to do it within the process. 编辑:澄清,我需要避免执行一些外部过程,而是寻找一些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)

Note: Make sure that your videos folder path consist of only valid video files as i didn't include any file validation in the above code snippet. 注意:请确保您的视频文件夹路径仅包含有效的视频文件,因为上述代码段中未包含任何文件验证。 Also, I have tested this code for a video file that contains one video stream and one audio stream. 另外,我已经针对包含一个视频流和一个音频流的视频文件测试了此代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM