Skip to content

Video editing with Python

feat: Make it possible when reading a file to select the audio track instead of taking the default one

Jimmy Huguete2babd8ffe75
diff --git a/moviepy/audio/io/AudioFileClip.py b/moviepy/audio/io/AudioFileClip.py
index 67c30d06..c45e292f 100644
--- a/moviepy/audio/io/AudioFileClip.py
+++ b/moviepy/audio/io/AudioFileClip.py
@@ -39,6 +39,9 @@ class AudioFileClip(AudioClip):
buffersize
See Parameters.
+ audio_stream_index
+ The index of the audio stream to read from the file.
+
Lifetime
--------
@@ -57,7 +60,7 @@ class AudioFileClip(AudioClip):
@convert_path_to_string("filename")
def __init__(
- self, filename, decode_file=False, buffersize=200000, nbytes=2, fps=44100
+ self, filename, decode_file=False, buffersize=200000, nbytes=2, fps=44100, audio_stream_index=0
):
AudioClip.__init__(self)
@@ -68,6 +71,7 @@ class AudioFileClip(AudioClip):
fps=fps,
nbytes=nbytes,
buffersize=buffersize,
+ audio_stream_index=audio_stream_index,
)
self.fps = fps
self.duration = self.reader.duration
diff --git a/moviepy/audio/io/readers.py b/moviepy/audio/io/readers.py
index 253a3bed..2f112860 100644
--- a/moviepy/audio/io/readers.py
+++ b/moviepy/audio/io/readers.py
@@ -36,6 +36,9 @@ class FFMPEG_AudioReader:
nbytes
Desired number of bytes (1,2,4) in the signal that will be
received from ffmpeg
+
+ audio_stream_index
+ The index of the audio stream to read from the file.
"""
def __init__(
@@ -47,6 +50,7 @@ class FFMPEG_AudioReader:
fps=44100,
nbytes=2,
nchannels=2,
+ audio_stream_index=0,
):
# TODO bring FFMPEG_AudioReader more in line with FFMPEG_VideoReader
# E.g. here self.pos is still 1-indexed.
@@ -62,6 +66,7 @@ class FFMPEG_AudioReader:
self.bitrate = infos["audio_bitrate"]
self.infos = infos
self.proc = None
+ self.audio_stream_index = audio_stream_index
self.n_frames = int(self.fps * self.duration)
self.buffersize = min(self.n_frames + 1, buffersize)
@@ -81,6 +86,8 @@ class FFMPEG_AudioReader:
"%.05f" % (start_time - offset),
"-i",
ffmpeg_escape_filename(self.filename),
+ "-map",
+ "0:a:%d" % self.audio_stream_index,
"-vn",
"-ss",
"%.05f" % offset,
diff --git a/moviepy/video/io/VideoFileClip.py b/moviepy/video/io/VideoFileClip.py
index 0eccdb58..6e093ab1 100644
--- a/moviepy/video/io/VideoFileClip.py
+++ b/moviepy/video/io/VideoFileClip.py
@@ -60,6 +60,9 @@ class VideoFileClip(VideoClip):
is_mask
`True` if the clip is going to be used as a mask.
+ audio_stream_index
+ The index of the audio stream to read from the file.
+
Attributes
----------
@@ -99,6 +102,7 @@ class VideoFileClip(VideoClip):
fps_source="fps",
pixel_format=None,
is_mask=False,
+ audio_stream_index=0,
):
VideoClip.__init__(self, is_mask=is_mask)
@@ -146,6 +150,7 @@ class VideoFileClip(VideoClip):
buffersize=audio_buffersize,
fps=audio_fps,
nbytes=audio_nbytes,
+ audio_stream_index=audio_stream_index,
)
def __deepcopy__(self, memo):