2

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:

....
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.

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.

user972014
  • 3,296
  • 6
  • 49
  • 89

1 Answers1

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)

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.

Vencat
  • 1,272
  • 11
  • 36