Skip to content

Video editing with Python

fixed bugs in ffmpeg_reader, refactorized reinit and init.

Zulkoba3a35d24f3e
diff --git a/moviepy/audio/io/readers.py b/moviepy/audio/io/readers.py
index 261c4c86..d3f18c7a 100644
--- a/moviepy/audio/io/readers.py
+++ b/moviepy/audio/io/readers.py
@@ -52,6 +52,8 @@ 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. """
@@ -77,11 +79,15 @@ class FFMPEG_AudioReader:
stderr=sp.PIPE)
self.pos = int(self.fps*starttime)
+
+
def skip_chunk(self,chunksize):
s = self.proc.stdout.read(self.nchannels*chunksize*self.nbytes)
self.proc.stdout.flush()
self.pos = self.pos+chunksize
+
+
def read_chunk(self,chunksize):
L = self.nchannels*chunksize*self.nbytes
s = self.proc.stdout.read(L)
diff --git a/moviepy/video/fx/resize.py b/moviepy/video/fx/resize.py
index 7d406df6..90495351 100644
--- a/moviepy/video/fx/resize.py
+++ b/moviepy/video/fx/resize.py
@@ -29,13 +29,11 @@ except ImportError:
except ImportError:
try:
-
- import scipy.misc.imresize as imresize
+ from scipy.misc import imresize
resizer = lambda pic, newsize : imresize(pic,
map(int, newsize[::-1]))
except ImportError:
-
resize_possible = False
diff --git a/moviepy/video/io/ffmpeg_reader.py b/moviepy/video/io/ffmpeg_reader.py
index 26c50bfa..fcee32a1 100644
--- a/moviepy/video/io/ffmpeg_reader.py
+++ b/moviepy/video/io/ffmpeg_reader.py
@@ -25,6 +25,7 @@ class FFMPEG_VideoReader:
w, h = self.size
bufsize = self.depth * w * h + 100
+ self.proc= None
self.bufsize= bufsize
self.initialize()
@@ -32,20 +33,31 @@ class FFMPEG_VideoReader:
self.pos = 1
self.lastread = self.read_frame()
- def initialize(self):
+ def initialize(self, starttime=0):
""" Opens the file, creates the pipe. """
-
- cmd = [FFMPEG_BINARY, '-i', self.filename,
- '-f', 'image2pipe',
+
+ self.close_proc() # if any
+
+ if starttime !=0 :
+ offset = min(1,starttime)
+ i_arg = ['-ss', "%.03f" % (starttime - offset),
+ '-i', self.filename,
+ '-ss', "%.03f" % offset]
+ else:
+ i_arg = [ '-i', self.filename]
+
+
+ cmd = ([FFMPEG_BINARY]+ i_arg +
+ ['-f', 'image2pipe',
"-pix_fmt", self.pix_fmt,
- '-vcodec', 'rawvideo', '-']
- if hasattr(self,'proc'):
- self.proc.kill()
- del self.proc
+ '-vcodec', 'rawvideo', '-'])
+
+
self.proc = sp.Popen(cmd, bufsize= self.bufsize,
stdin=sp.PIPE,
stdout=sp.PIPE,
stderr=sp.PIPE)
+
def load_infos(self, print_infos=False):
""" reads the FFMPEG info on the file and sets self.size
@@ -84,11 +96,14 @@ class FFMPEG_VideoReader:
self.duration = cvsecs(*hms)
self.nframes = int(self.duration*self.fps)
- 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
def skip_frames(self, n=1):
""" Reads and throws away n frames """
@@ -106,6 +121,8 @@ class FFMPEG_VideoReader:
# if it does, raise an error.
nbytes= self.depth*w*h
s = self.proc.stdout.read(nbytes)
+ #self.proc.terminate()
+ #print( self.proc.stderr.read(10000))
assert len(s) == nbytes
result = np.fromstring(s,
dtype='uint8').reshape((h, w, len(s)//(w*h)))
@@ -120,24 +137,6 @@ class FFMPEG_VideoReader:
return result
- def reinitialize(self, starttime=0):
- """ Restarts the reading, starts at an arbitrary
- location (!! SLOW !!) """
- self.close()
- if starttime == 0:
- self.initialize()
- else:
- offset = min(1, starttime)
- cmd = [FFMPEG_BINARY, '-ss', "%.03f" % (starttime - offset),
- '-i', self.filename,
- '-ss', "%.03f" % offset,
- '-f', 'image2pipe',
- "-pix_fmt", self.pix_fmt,
- '-vcodec', 'rawvideo', '-']
- self.proc = sp.Popen(cmd, stdin=sp.PIPE,
- stdout=sp.PIPE,
- stderr=sp.PIPE)
-
def get_frame(self, t):
""" Reads a frame at time t. Note for coders:
getting an arbitrary frame in the video with ffmpeg can be
@@ -155,7 +154,7 @@ class FFMPEG_VideoReader:
return self.lastread
else:
if(pos < self.pos) or (pos > self.pos+100):
- self.reinitialize(t)
+ self.initialize(t)
else:
self.skip_frames(pos-self.pos-1)
result = self.read_frame()
diff --git a/moviepy/video/io/preview.py b/moviepy/video/io/preview.py
index 72bd91a5..b7df17d8 100644
--- a/moviepy/video/io/preview.py
+++ b/moviepy/video/io/preview.py
@@ -99,7 +99,7 @@ def preview(clip, fps=15, audio=True, audio_fps=22050,
if audio:
videoFlag.clear()
- print "Keyboard interrupt"
+ print( "Keyboard interrupt" )
return result
elif event.type == pg.MOUSEBUTTONDOWN:
@@ -107,8 +107,8 @@ def preview(clip, fps=15, audio=True, audio_fps=22050,
rgb = img[y,x]
result.append({'time':t, 'position':(x,y),
'color':rgb})
- print "time, position, color : ", "%.03f, %s, %s"%(
- t,str((x,y)),str(rgb))
+ print( "time, position, color : ", "%.03f, %s, %s"%(
+ t,str((x,y)),str(rgb)))
t1 = time.time()
time.sleep(max(0, t - (t1-t0)) )
@@ -123,14 +123,14 @@ def image_preview(clip):
if event.type == pg.KEYDOWN:
if (event.key == pg.K_ESCAPE):
videoFlag.clear()
- print "Keyboard interrupt"
+ print( "Keyboard interrupt" )
return result
elif event.type == pg.MOUSEBUTTONDOWN:
x,y = pg.mouse.get_pos()
rgb = img[y,x]
result.append({'time':t, 'position':(x,y),
'color':rgb})
- print "time, position, color : ", "%.03f, %s, %s"%(
- t,str((x,y)),str(rgb))
+ print( "time, position, color : ", "%.03f, %s, %s"%(
+ t,str((x,y)),str(rgb)))
return result
diff --git a/setup.py b/setup.py
index 82a05822..6ce11368 100644
--- a/setup.py
+++ b/setup.py
@@ -6,7 +6,7 @@ ez_setup.use_setuptools()
from setuptools import setup, find_packages
setup(name='moviepy',
- version='0.2.1.7',
+ version='0.2.1.7.02',
author='Zulko 2013',
description='Module for script-based video editing',
long_description=open('README.rst').read(),