Skip to content

Commit

Permalink
removed all buffer "flush". Made check_duration optional in ffmpeg_pa…
Browse files Browse the repository at this point in the history
…rse_info. Corrected bug in speedx
  • Loading branch information
Zulko committed Sep 18, 2014
1 parent e31e778 commit ee10297
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
2 changes: 1 addition & 1 deletion moviepy/audio/io/readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def read_chunk(self,chunksize):
result = (1.0*result / 2**(8*self.nbytes-1)).\
reshape((len(result)/self.nchannels,
self.nchannels))
self.proc.stdout.flush()
#self.proc.stdout.flush()
self.pos = self.pos+chunksize
return result

Expand Down
5 changes: 2 additions & 3 deletions moviepy/video/fx/speedx.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from moviepy.decorators import apply_to_mask,apply_to_audio

@apply_to_mask
@apply_to_audio

def speedx(clip, factor = None, final_duration=None):
"""
Returns a clip playing the current clip but at a speed multiplied
Expand All @@ -14,7 +13,7 @@ def speedx(clip, factor = None, final_duration=None):
if final_duration:
factor = 1.0* clip.duration / final_duration

newclip = clip.fl_time(lambda t: factor * t)
newclip = clip.fl_time(lambda t: factor * t, apply_to=['mask', 'audio'])

if clip.duration != None:
newclip = newclip.set_duration(1.0 * clip.duration / factor)
Expand Down
42 changes: 24 additions & 18 deletions moviepy/video/io/ffmpeg_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
class FFMPEG_VideoReader:

def __init__(self, filename, print_infos=False, bufsize = None,
pix_fmt="rgb24"):
pix_fmt="rgb24", check_duration=True):

self.filename = filename
infos = ffmpeg_parse_infos(filename, print_infos)
infos = ffmpeg_parse_infos(filename, print_infos, check_duration)
self.fps = infos['video_fps']
self.size = infos['video_size']
self.duration = infos['video_duration']
Expand Down Expand Up @@ -80,7 +80,7 @@ def skip_frames(self, n=1):
w, h = self.size
for i in range(n):
self.proc.stdout.read(self.depth*w*h)
self.proc.stdout.flush()
#self.proc.stdout.flush()
self.pos += n


Expand Down Expand Up @@ -180,12 +180,12 @@ def ffmpeg_read_image(filename, with_mask=True):
pix_fmt = 'rgba'
else:
pix_fmt = "rgb24"
reader = FFMPEG_VideoReader(filename, pix_fmt=pix_fmt)
reader = FFMPEG_VideoReader(filename, pix_fmt=pix_fmt, check_duration=False)
im = reader.lastread
del reader
return im

def ffmpeg_parse_infos(filename, print_infos=False):
def ffmpeg_parse_infos(filename, print_infos=False, check_duration=True):
"""Get file infos using ffmpeg.
Returns a dictionnary with the fields:
Expand Down Expand Up @@ -228,15 +228,18 @@ def ffmpeg_parse_infos(filename, print_infos=False):


# get duration (in seconds)
try:
keyword = ('frame=' if is_GIF else 'Duration: ')
line = [l for l in lines if keyword in l][0]
match = re.findall("([0-9][0-9]:[0-9][0-9]:[0-9][0-9].[0-9][0-9])", line)[0]
result['duration'] = cvsecs(match)
except:
raise IOError(("MoviePy error: failed to read the duration of file %s.\n"
"Here are the file infos returned by ffmpeg:\n\n%s")%(
filename, infos))
result['duration'] = None

if check_duration:
try:
keyword = ('frame=' if is_GIF else 'Duration: ')
line = [l for l in lines if keyword in l][0]
match = re.findall("([0-9][0-9]:[0-9][0-9]:[0-9][0-9].[0-9][0-9])", line)[0]
result['duration'] = cvsecs(match)
except:
raise IOError(("MoviePy error: failed to read the duration of file %s.\n"
"Here are the file infos returned by ffmpeg:\n\n%s")%(
filename, infos))

# get the output line that speaks about video
lines_video = [l for l in lines if ' Video: ' in l]
Expand Down Expand Up @@ -275,10 +278,13 @@ def ffmpeg_parse_infos(filename, print_infos=False):
for x in [23,24,25,30,50]:
if (fps!=x) and abs(fps - x*coef) < .01:
result['video_fps'] = x*coef

result['video_nframes'] = int(result['duration']*result['video_fps'])+1

result['video_duration'] = result['duration']

if check_duration:
result['video_nframes'] = int(result['duration']*result['video_fps'])+1
result['video_duration'] = result['duration']
else:
result['video_nframes'] = 1
result['video_duration'] = None
# We could have also recomputed the duration from the number
# of frames, as follows:
# >>> result['video_duration'] = result['video_nframes'] / result['video_fps']
Expand Down

0 comments on commit ee10297

Please sign in to comment.