簡體   English   中英

Python從視頻(音頻/視頻)獲取流列表

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

我有一個視頻文件,我想從中獲取流的列表。 我可以通過執行一個簡單的`ffprobe video.mp4來查看所需的結果:

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

但是我需要使用在Windows和Ubuntu上都可以運行的python和代碼, 而無需執行外部進程

我的真正目標是檢查視頻中是否有任何音頻流(簡單的是/否就足夠了),但是我認為獲取更多信息可以解決我的問題,因此我要詢問整個流

編輯:澄清,我需要避免執行一些外部過程,而是尋找一些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)

注意:請確保您的視頻文件夾路徑僅包含有效的視頻文件,因為上述代碼段中未包含任何文件驗證。 另外,我已經針對包含一個視頻流和一個音頻流的視頻文件測試了此代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM