Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow resizing frames in ffmpeg when reading #489

Merged
4 commits merged into from
Mar 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions moviepy/video/io/VideoFileClip.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ class VideoFileClip(VideoClip):
audio:
Set to `False` if the clip doesn't have any audio or if you do not
wish to read the audio.

target_resolution:
Set to (desired_height, desired_width) to have ffmpeg resize the frames
before returning them. This is much faster than streaming in high-res
and then resizing. If either dimension is None, the frames are resized
by keeping the existing aspect ratio.

resize_algorithm:
The algorithm used for resizing. Default: "bicubic", other popular
options include "bilinear" and "fast_bilinear". For more information, see
https://ffmpeg.org/ffmpeg-scaler.html


Attributes
-----------
Expand All @@ -48,14 +60,18 @@ class VideoFileClip(VideoClip):

def __init__(self, filename, has_mask=False,
audio=True, audio_buffersize = 200000,
target_resolution=None, resize_algorithm='bicubic',
audio_fps=44100, audio_nbytes=2, verbose=False):

VideoClip.__init__(self)

# Make a reader
pix_fmt= "rgba" if has_mask else "rgb24"
self.reader = None #need this just in case FFMPEG has issues (__del__ complains)
self.reader = FFMPEG_VideoReader(filename, pix_fmt=pix_fmt)
self.reader = None # need this just in case FFMPEG has issues (__del__ complains)
self.reader = FFMPEG_VideoReader(filename, pix_fmt=pix_fmt,
target_resolution=target_resolution,
resize_algo=resize_algorithm)

# Make some of the reader's attributes accessible from the clip
self.duration = self.reader.duration
self.end = self.reader.duration
Expand Down
26 changes: 21 additions & 5 deletions moviepy/video/io/ffmpeg_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,28 @@
class FFMPEG_VideoReader:

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

self.filename = filename
infos = ffmpeg_parse_infos(filename, print_infos, check_duration)
self.fps = infos['video_fps']
self.size = infos['video_size']

if target_resolution:
# revert the order, as ffmpeg used (width, height)
target_resolution = target_resolution[1], target_resolution[0]

if None in target_resolution:
ratio = 1
for idx, target in enumerate(target_resolution):
if target:
ratio = target / self.size[idx]
self.size = (int(self.size[0] * ratio), int(self.size[1] * ratio))
else:
self.size = target_resolution
self.resize_algo = resize_algo

self.duration = infos['video_duration']
self.ffmpeg_duration = infos['duration']
self.nframes = infos['video_nframes']
Expand Down Expand Up @@ -66,13 +82,13 @@ def initialize(self, starttime=0):
else:
i_arg = [ '-i', self.filename]


cmd = ([get_setting("FFMPEG_BINARY")]+ i_arg +
['-loglevel', 'error',
cmd = ([get_setting("FFMPEG_BINARY")] + i_arg +
['-loglevel', 'error',
'-f', 'image2pipe',
'-vf', 'scale=%d:%d' % tuple(self.size),
'-sws_flags', self.resize_algo,
"-pix_fmt", self.pix_fmt,
'-vcodec', 'rawvideo', '-'])

popen_params = {"bufsize": self.bufsize,
"stdout": sp.PIPE,
"stderr": sp.PIPE,
Expand Down
23 changes: 23 additions & 0 deletions tests/test_VideoFileClip.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,28 @@ def test_setup():
assert clip.fps == 30
assert clip.size == [1024*3, 800]

def test_ffmpeg_resizing():
# Test downscaling
target_resolution = (128,128)
video = VideoFileClip("media/big_buck_bunny_432_433.webm", target_resolution=target_resolution)
frame = video.get_frame(0)
assert frame.shape[0:2] == target_resolution

target_resolution = (128, None)
video = VideoFileClip("media/big_buck_bunny_432_433.webm", target_resolution=target_resolution)
frame = video.get_frame(0)
assert frame.shape[0] == target_resolution[0]

target_resolution = (None, 128)
video = VideoFileClip("media/big_buck_bunny_432_433.webm", target_resolution=target_resolution)
frame = video.get_frame(0)
assert frame.shape[1] == target_resolution[1]

# Test upscaling
target_resolution = (None, 2048)
video = VideoFileClip("media/big_buck_bunny_432_433.webm", target_resolution=target_resolution)
frame = video.get_frame(0)
assert frame.shape[1] == target_resolution[1]

if __name__ == '__main__':
pytest.main()