From ada3bbe3467b185f477219701051b06b6a392488 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Thu, 25 Sep 2014 15:28:08 +0200 Subject: [PATCH] add optoin to use melt(MLT) to extract frames from H264 --- README | 2 +- pandora/archive/extract.py | 51 ++++++++++++++++++++++++++++++-------- pandora/item/models.py | 2 +- pandora/settings.py | 1 + vm/firstboot.sh | 1 + 5 files changed, 44 insertions(+), 13 deletions(-) diff --git a/README b/README index 7e241171c..807d890a7 100644 --- a/README +++ b/README @@ -29,7 +29,7 @@ To run pan.do/ra you need to install and setup: python-geoip python-html5lib python-lxml \ python-gst0.10 gstreamer0.10-plugins-good gstreamer0.10-plugins-bad \ postgresql postgresql-contrib rabbitmq-server \ - ffmpeg2theora libav-tools libavcodec-extra-53 \ + ffmpeg2theora libav-tools libavcodec-extra-53 melt \ python-ox oxframe imagemagick poppler-utils mkvtoolnix gpac diff --git a/pandora/archive/extract.py b/pandora/archive/extract.py index eef7d8451..266141aab 100644 --- a/pandora/archive/extract.py +++ b/pandora/archive/extract.py @@ -325,7 +325,7 @@ def run_command(cmd, timeout=10): return p.returncode -def frame(video, frame, position, height=128, redo=False): +def frame(video, frame, position, height=128, redo=False, info=None): ''' params: video input @@ -339,21 +339,50 @@ def frame(video, frame, position, height=128, redo=False): if redo or not exists(frame): ox.makedirs(folder) if video.endswith('.mp4'): - cmd = [ - AVCONV, '-y', - '-ss', str(position), - '-i', video, - '-an', '-vframes', '1', - '-vf', 'scale=-1:%s' % height - ] - if not frame.endswith('.png'): - cmd += ['-f', 'mjpeg'] - cmd += [frame] + if settings.USE_MELT: + cmd = melt_frame_cmd(video, frame, position, height, info) + else: + cmd = melt_frame_cmd(video, frame, position, height) else: cmd = ['oxframe', '-i', video, '-o', frame, '-p', str(position), '-y', str(height)] run_command(cmd) +def avconv_frame_cmd(video, frame, position, height=128): + cmd = [ + AVCONV, '-y', + '-ss', str(position), + '-i', video, + '-an', '-vframes', '1', + '-vf', 'scale=-1:%s' % height + ] + if not frame.endswith('.png'): + cmd += ['-f', 'mjpeg'] + cmd += [frame] + return cmd + +def melt_frame_cmd(video, frame, position, height=128, info=None): + if not info: + info = ox.avinfo(video) + fps = float(AspectRatio(info['video'][0]['framerate'])) + format = frame.split('.')[-1] + vcodec = 'mjpeg' if format == 'jpg' else 'png' + cmd = [ + 'melt', + '-silent', + video, + 'in=%d' % position, 'out=%d' % position, + '-consumer' 'avformat:%s' % frame, + 'vcodec=%s' % vcodec + ] + if height: + dar = AspectRatio(info['video'][0]['display_aspect_ratio']) + width = int(dar * height) + width += width % 2 + position = int(position * fps) + cmd += ['-s', '%sx%s' % (width, height)] + return cmd + def frame_direct(video, target, position): fdir = os.path.dirname(target) if fdir and not os.path.exists(fdir): diff --git a/pandora/item/models.py b/pandora/item/models.py index bbef5001f..7dcbec15a 100644 --- a/pandora/item/models.py +++ b/pandora/item/models.py @@ -1084,7 +1084,7 @@ class Item(models.Model): path = os.path.join(settings.MEDIA_ROOT, stream.path(), 'frames', "%dp"%height, "%s.jpg"%position) if not os.path.exists(path) and stream.media: - extract.frame(stream.media.path, path, position, height) + extract.frame(stream.media.path, path, position, height, info=stream.info) if not os.path.exists(path): return None return path diff --git a/pandora/settings.py b/pandora/settings.py index 374fbfb49..49cb4128a 100644 --- a/pandora/settings.py +++ b/pandora/settings.py @@ -152,6 +152,7 @@ LOGGING = { AUTH_PROFILE_MODULE = 'user.UserProfile' AUTH_CHECK_USERNAME = True AVCONV_VERSION = 0 +USE_MELT=False #========================================================================= #Pan.do/ra related settings settings diff --git a/vm/firstboot.sh b/vm/firstboot.sh index ad80109ae..60ddecc08 100755 --- a/vm/firstboot.sh +++ b/vm/firstboot.sh @@ -52,6 +52,7 @@ apt-get install -y \ $LIBAVCODEC_EXTRA \ libav-tools \ ffmpeg2theora \ + melt \ mkvtoolnix \ gpac \ imagemagick \