Skip to content

Video editing with Python

corrected set_duration audio.duration bug. added stderr.close in ffmpeg_writer

Zulko20d7e2077e72
diff --git a/moviepy/Clip.py b/moviepy/Clip.py
index 34267df7..78cc33c7 100644
--- a/moviepy/Clip.py
+++ b/moviepy/Clip.py
@@ -51,18 +51,29 @@ class Clip:
def copy(self):
- """
- Shallow copy of the clip. This method is intensively used
- to produce new clips every time there is an outplace
- transformation of the clip (clip.resize, clip.subclip, etc.)
+ """ Shallow copy of the clip.
+
+ Returns a shwallow copy of the clip whose mask and audio will
+ be shallow copies of the clip's mask and audio if they exist.
+
+ This method is intensively used to produce new clips every time
+ there is an outplace transformation of the clip (clip.resize,
+ clip.subclip, etc.)
"""
- return copy(self)
+ newclip = copy(self)
+ if hasattr(self, 'audio'):
+ newclip.audio = copy(self.audio)
+ if hasattr(self, 'mask'):
+ newclip.mask = copy(self.mask)
+
+ return newclip
def fl(self, fun, apply_to=[] , keep_duration=True):
- """
+ """ General processing of a clip.
+
Returns a new Clip whose frames are a transformation
(through function ``fun``) of the frames of the current clip.
@@ -325,10 +336,11 @@ class Clip:
"be taken from clips with a ``duration``")
else:
tb = self.duration - tb
- if (tb != None):
- return newclip.set_duration(tb - ta)
- else:
- return newclip
+ if (tb is not None):
+ newclip.duration = tb - ta
+ newclip.end = newclip.start + newclip.duration
+
+ return newclip
diff --git a/moviepy/audio/io/ffmpeg_audiowriter.py b/moviepy/audio/io/ffmpeg_audiowriter.py
index f7637707..952179e6 100644
--- a/moviepy/audio/io/ffmpeg_audiowriter.py
+++ b/moviepy/audio/io/ffmpeg_audiowriter.py
@@ -63,6 +63,7 @@ class FFMPEG_AudioWriter:
def close(self):
self.proc.stdin.close()
+ self.proc.stderr.close()
self.proc.wait()
del self.proc
@@ -92,7 +93,7 @@ def ffmpeg_audiowrite(clip, filename, fps, nbytes, buffersize,
nchunks = totalsize // buffersize + 1
pospos = list(range(0, totalsize, buffersize))+[totalsize]
- for i in range(nchunks):
+ for i in tqdm(range(nchunks)):
tt = (1.0/fps)*np.arange(pospos[i],pospos[i+1])
sndarray = clip.to_soundarray(tt,nbytes)
writer.write_frames(sndarray)
diff --git a/moviepy/tools.py b/moviepy/tools.py
index 4f89dbf9..df100302 100644
--- a/moviepy/tools.py
+++ b/moviepy/tools.py
@@ -11,8 +11,7 @@ def sys_write_flush(s):
sys.stdout.flush()
-def subprocess_call(cmd, stdout=None, stdin = None,
- stderr = sp.PIPE, verbose=True, errorprint=True):
+def subprocess_call(cmd, verbose=True, errorprint=True):
"""
executes the subprocess command
"""
@@ -22,20 +21,21 @@ def subprocess_call(cmd, stdout=None, stdin = None,
verboseprint( "\nMoviePy Running:\n>>> "+ " ".join(cmd) )
- proc = sp.Popen(cmd, stdout=stdout,
- stdin = stdin,
- stderr = stderr )
+ proc = sp.Popen(cmd, stderr = sp.PIPE)
- proc.communicate() # proc.wait()
+ out, err = proc.communicate() # proc.wait()
+ proc.stderr.close()
if proc.returncode:
if errorprint:
sys_write_flush( "\nMoviePy: WARNING !\n"
" The following command returned an error:\n")
- sys_write_flush( proc.stderr.read().decode('utf8'))
+ sys_write_flush( err.decode('utf8'))
raise IOError
else:
verboseprint( "\n... command successful.\n")
+
+ del proc
def cvsecs(*args):
diff --git a/moviepy/video/VideoClip.py b/moviepy/video/VideoClip.py
index 50ad3ae3..363fd4f1 100644
--- a/moviepy/video/VideoClip.py
+++ b/moviepy/video/VideoClip.py
@@ -417,7 +417,7 @@ class VideoClip(Clip):
filename]
- subprocess_call( cmd )
+ subprocess_call( cmd, verbose = verbose )
for f in tempfiles:
os.remove(f)
diff --git a/moviepy/video/io/ffmpeg_reader.py b/moviepy/video/io/ffmpeg_reader.py
index 42cc97c7..0e4d264e 100644
--- a/moviepy/video/io/ffmpeg_reader.py
+++ b/moviepy/video/io/ffmpeg_reader.py
@@ -36,7 +36,7 @@ class FFMPEG_VideoReader:
def initialize(self, starttime=0):
"""Opens the file, creates the pipe. """
- self.close_proc() # if any
+ self.close() # if any
if starttime !=0 :
offset = min(1,starttime)
@@ -155,7 +155,7 @@ class FFMPEG_VideoReader:
self.pos = pos
return result
- def close_proc(self):
+ def close(self):
if self.proc is not None:
self.proc.terminate()
self.proc.stdout.close()
@@ -163,7 +163,7 @@ class FFMPEG_VideoReader:
del self.proc
def __del__(self):
- self.close_proc()
+ self.close()
del self.lastread
diff --git a/moviepy/video/io/ffmpeg_tools.py b/moviepy/video/io/ffmpeg_tools.py
index 0d74ecae..76e9a39d 100644
--- a/moviepy/video/io/ffmpeg_tools.py
+++ b/moviepy/video/io/ffmpeg_tools.py
@@ -20,7 +20,7 @@ def ffmpeg_movie_from_frames(filename, folder, fps, digits=6):
"-r", "%d"%self.fps,
filename]
- subprocess_call(cmd, stdin=None, stdout=None)
+ subprocess_call(cmd)
def ffmpeg_extract_subclip(filename, t1, t2, targetname=None):
@@ -36,25 +36,26 @@ def ffmpeg_extract_subclip(filename, t1, t2, targetname=None):
"-ss", "%0.2f"%t1,
"-t", "%0.2f"%(t2-t1),
"-vcodec", "copy", "-acodec", "copy", targetname]
-
- subprocess_call(cmd, stdin=None, stdout=None)
+
+ subprocess_call(cmd)
-def ffmpeg_merge_video_audio(video,audio,output,
- vcodec='copy', acodec='copy', ffmpeg_output=False):
+def ffmpeg_merge_video_audio(video,audio,output, vcodec='copy',
+ acodec='copy', ffmpeg_output=False,
+ verbose = True):
""" merges video file ``video`` and audio file ``audio`` into one
movie file ``output``. """
cmd = ["ffmpeg", "-y", "-i", audio,"-i", video,
"-vcodec", vcodec, "-acodec", acodec, output]
- subprocess_call(cmd, stdin=None, stdout=None)
+ subprocess_call(cmd, verbose = verbose)
def ffmpeg_extract_audio(inputfile,output,bitrate=3000,fps=44100):
""" extract the sound from a video file and save it in ``output`` """
cmd = ["ffmpeg", "-y", "-i", inputfile, "-ab", "%dk"%bitrate,
"-ar", "%d"%fps, output]
- subprocess_call(cmd, stdin=None, stdout=None)
+ subprocess_call(cmd)
def ffmpeg_resize(video,output,size):
@@ -63,5 +64,5 @@ def ffmpeg_resize(video,output,size):
cmd= ["ffmpeg", "-i", video, "-vf", "scale=%d:%d"%(res[0], res[1]),
output]
- subprocess_call(cmd, stdin=None, stdout=None)
+ subprocess_call(cmd)
diff --git a/moviepy/video/io/ffmpeg_writer.py b/moviepy/video/io/ffmpeg_writer.py
index 1875772f..cf3a2f7f 100644
--- a/moviepy/video/io/ffmpeg_writer.py
+++ b/moviepy/video/io/ffmpeg_writer.py
@@ -75,8 +75,10 @@ class FFMPEG_VideoWriter:
self.proc.stdin.write(img_array.tostring())
def close(self):
- out, err = self.proc.communicate() # proc.wait()
self.proc.stdin.close()
+ self.proc.stderr.close()
+ self.proc.wait()
+
del self.proc
def ffmpeg_write_video(clip, filename, fps, codec="libx264", bitrate=None,