Skip to content

Video editing with Python

improved audio reading. Fixed bug at rewind (?)

Zulko2a5393651dd3
diff --git a/moviepy/audio/io/AudioFileClip.py b/moviepy/audio/io/AudioFileClip.py
index 33cd305f..2e2afd3a 100644
--- a/moviepy/audio/io/AudioFileClip.py
+++ b/moviepy/audio/io/AudioFileClip.py
@@ -10,7 +10,6 @@ from moviepy.audio.io.readers import FFMPEG_AudioReader
class AudioFileClip(AudioClip):
"""
-
An audio clip read from a sound file, or an array.
The whole file is not loaded in memory. Instead, only a portion is
read and stored in memory. this portion includes frames before
@@ -74,13 +73,14 @@ class AudioFileClip(AudioClip):
try:
result[in_time] = self.buffer[inds - self._fstart_buffer]
return result
- except:
+ except IndexError:
print ("Error: wrong indices in video buffer. Maybe"+
" buffer too small.")
- raise
+ raise IndexError
+
else:
- ind = int(self.fps*t)#+1
- if ind<1 or ind> self.nframes: # out of time: return 0
+ ind = int(self.fps*t)
+ if ind<0 or ind> self.nframes: # out of time: return 0
return np.zeros(self.nchannels)
if not (0 <= (ind - self._fstart_buffer) <len(self.buffer)):
@@ -107,7 +107,8 @@ class AudioFileClip(AudioClip):
"""
# start frame for the buffer
fbuffer = framenumber - self.buffersize//2
- fbuffer = max(1, fbuffer)
+
+ fbuffer = max(0, fbuffer)
if (self.buffer!=None):
diff --git a/moviepy/audio/io/readers.py b/moviepy/audio/io/readers.py
index 43b16390..e84b6786 100644
--- a/moviepy/audio/io/readers.py
+++ b/moviepy/audio/io/readers.py
@@ -10,7 +10,9 @@ class FFMPEG_AudioReader:
""" streams any file (audio or video) and outputs a 16bit 44100HZ
wav that can be read by the moviepy AudioFileClip. """
- def __init__(self, filename, bufsize, print_infos=False, fps=44100, nbytes=2):
+ def __init__(self, filename, bufsize, print_infos=False,
+ fps=44100, nbytes=2):
+
self.filename = filename
self.nbytes = nbytes
self.bufsize=bufsize
@@ -19,27 +21,8 @@ class FFMPEG_AudioReader:
self.acodec = 'pcm_s%dle'%(8*nbytes)
self.nchannels = 2
self.load_infos()
+ self.proc = None
self.initialize()
-
-
- def initialize(self):
- """ Opens the file, creates the pipe. """
-
- cmd = [ FFMPEG_BINARY, '-i', self.filename, '-vn',
- '-f', self.f,
- '-acodec', self.acodec,
- '-ar', "%d"%self.fps,
- '-ac', '%d'%self.nchannels, '-']
-
- if hasattr(self, 'proc'):
- self.proc.kill()
- del self.proc
-
- self.proc = sp.Popen( cmd, bufsize=self.bufsize,
- stdin=sp.PIPE,
- stdout=sp.PIPE,
- stderr=sp.PIPE)
- self.pos = 1
def load_infos(self, print_infos=False):
""" reads the FFMPEG info on the file and sets self.size
@@ -69,27 +52,32 @@ class FFMPEG_AudioReader:
self.duration = cvsecs(*hms)
self.nframes = int(self.duration*self.fps)
+ def initialize(self, starttime = 0):
+ """ Opens the file, creates the pipe. """
- def reinitialize(self,starttime=0):
- """ Restarts the reading, starts at an arbitrary
- location (!! SLOW !!) """
- self.close()
- if starttime==0:
- self.initialize()
- else:
+ self.close_proc() # if any
+
+ if starttime !=0 :
offset = min(1,starttime)
- cmd = [ FFMPEG_BINARY,
+ i_arg = [ FFMPEG_BINARY,
"-ss", "%.05f"%(starttime-offset),
'-i', self.filename, '-vn',
- "-ss", "%.05f"%offset,
- '-f', self.f,
- '-acodec', self.acodec,
- '-ar', "%d"%self.fps,
- '-ac', '%d'%self.nchannels, '-']
- self.proc = sp.Popen(cmd, stdin=sp.PIPE,
- stdout=sp.PIPE,
- stderr=sp.PIPE)
- self.pos = int(self.fps*starttime)+1
+ "-ss", "%.05f"%offset]
+ else:
+ i_arg = [ '-i', self.filename, '-vn']
+
+
+ cmd = ([FFMPEG_BINARY] + i_arg +
+ ['-f', self.f,
+ '-acodec', self.acodec,
+ '-ar', "%d"%self.fps,
+ '-ac', '%d'%self.nchannels, '-'])
+
+ self.proc = sp.Popen( cmd, bufsize=self.bufsize,
+ stdin=sp.PIPE,
+ stdout=sp.PIPE,
+ stderr=sp.PIPE)
+ self.pos = int(self.fps*starttime)
def skip_chunk(self,chunksize):
s = self.proc.stdout.read(self.nchannels*chunksize*self.nbytes)
@@ -116,16 +104,20 @@ class FFMPEG_AudioReader:
"""
if (pos < self.pos) or (pos> (self.pos+1000000)):
t = 1.0*pos/self.fps
- self.reinitialize(t)
+ self.initialize(t)
elif pos > self.pos:
self.skip_chunk(pos-self.pos)
+ # last case standing: pos = current pos
self.pos = pos
- def close(self):
- self.proc.terminate()
- for std in self.proc.stdin,self.proc.stdout,self.proc.stderr:
- std.close()
- del self.proc
+ def close_proc(self):
+ if self.proc is not None:
+ self.proc.terminate()
+ for std in [self.proc.stdin,
+ self.proc.stdout,
+ self.proc.stderr]:
+ std.close()
+ del self.proc
diff --git a/moviepy/video/VideoClip.py b/moviepy/video/VideoClip.py
index 1033c156..f14d4638 100644
--- a/moviepy/video/VideoClip.py
+++ b/moviepy/video/VideoClip.py
@@ -402,6 +402,9 @@ class VideoClip(Clip):
" 1 1 %d`"%len(tt) +
" -coalesce -fuzz %02d"%fuzz + "%"+
" -layers %s %s"%(opt,filename))
+
+ verboseprint("running %s"%cmd)
+
os.system(cmd)
elif program == "ffmpeg":
diff --git a/moviepy/video/io/VideoFileClip.py b/moviepy/video/io/VideoFileClip.py
index 7fc9b614..8b37213a 100644
--- a/moviepy/video/io/VideoFileClip.py
+++ b/moviepy/video/io/VideoFileClip.py
@@ -61,7 +61,8 @@ class VideoFileClip(VideoClip):
nbytes = audio_nbytes)
def coreader(self, audio=True):
- """ Returns a copy of the AudioFileClip, i.e. a new entrance point
- to the video file. Use copy when you have different clips
- watching the same video file at different times. """
+ """ Returns a copy of the AudioFileClip, i.e. a
+ new entrance point to the video file. Use copy
+ when you have different clips watching the same
+ video file at different times. """
return VideoFileClip(**self.parameters)
diff --git a/moviepy/video/io/ffmpeg_reader.py b/moviepy/video/io/ffmpeg_reader.py
index 90c0baed..26c50bfa 100644
--- a/moviepy/video/io/ffmpeg_reader.py
+++ b/moviepy/video/io/ffmpeg_reader.py
@@ -23,7 +23,7 @@ class FFMPEG_VideoReader:
if bufsize is None:
w, h = self.size
- bufsize = self.depth * w * h +100
+ bufsize = self.depth * w * h + 100
self.bufsize= bufsize
self.initialize()