0

I'm trying to get the number of audio tracks in a video file. The video have multiple tracks (like different, selectable languages for the same movie.) So if there are three optional languages for the video, i'd like to get the number 3 in the end, no matter if the audio is in stereo, mono or in 5.1.

So far I tried to do it with moviepy. I found only the function "reader.nchannels", but that counts only the first audio track's left and right channel, so I get the number 2 every time.

The code right now is really simple, it looks like this:

from moviepy.editor import *
from moviepy.audio import *

clip = VideoFileClip(source)
audio_tracks = clip.audio.reader.nchannels

I also tried to get every info from the audio like this:

audio = AudioFileClip(source)
tracks= audio_tracks.reader.infos

The output for this looks like this:

"'audio_found': True, 'audio_fps': 48000}"
Arount
  • 9,853
  • 1
  • 30
  • 43
  • I don't think that this is possible in MoviePy at the moment. This part of the code is just a wrapper around FFmpeg, and whilst I assume that it is possible to detect 5.1/mono/stereo using FFmpeg, it hasn't been implemented in MoviePy. Feel free to have a look around the source code and perhaps look at implementing it yourself and creating a pull request to the main repo. – Tom Burrows Jun 19 '20 at 10:42

1 Answers1

0

tburrows13, thanks for pointing to the right direction.

I was able to get the numbers of audio channels and store it in a variable through a py script. Maybe this is not the most elegant solution, but it works, so here it is, if someone needs it. You have to import "subprocess" and use ffprobe with it. ffprobe comes with ffmpeg.

To get the number of streams the command goes like this:

ffprobe <filename here> -show_entries format=nb_streams

This will give you the number of streams in the file, not just the audios, but the video streams too. There is an option to get the data only for the audio streams, but this was not necessary for my project.

You can call this command through a python script. The command needs to be a string, you can store it in a variable too. To get and store the output of this commmand in an other variable you can use this:

variable = subprocess.check_output(subprocesscommand) # subprocesscommand is the string version of the command wrote above. 

If you print out now this variable the output will be something like: b'[FORMAT]\r\nnb_streams=3\r\n[/FORMAT]\r\n'

Now you just need to slice the string value, to get the number of the streams.

Thanks again for your help!