From 215b093fdf91624a1834c027ebcf5b318992e477 Mon Sep 17 00:00:00 2001 From: marcwebbie Date: Sun, 16 Oct 2016 02:30:46 -0200 Subject: [PATCH 1/3] Add FFmpegSlice postprocessor --- youtube_dl/__init__.py | 6 ++++++ youtube_dl/options.py | 8 ++++++++ youtube_dl/postprocessor/__init__.py | 2 ++ youtube_dl/postprocessor/ffmpeg.py | 24 +++++++++++++++++++++++- 4 files changed, 39 insertions(+), 1 deletion(-) diff --git a/youtube_dl/__init__.py b/youtube_dl/__init__.py index 643393558..80cc5df41 100644 --- a/youtube_dl/__init__.py +++ b/youtube_dl/__init__.py @@ -252,6 +252,12 @@ def _real_main(argv=None): 'preferredquality': opts.audioquality, 'nopostoverwrites': opts.nopostoverwrites, }) + if opts.slicestart or opts.sliceend: + postprocessors.append({ + 'key': 'FFmpegSlice', + 'slice_start_time': opts.slicestart, + 'slice_end_time': opts.sliceend, + }) if opts.recodevideo: postprocessors.append({ 'key': 'FFmpegVideoConvertor', diff --git a/youtube_dl/options.py b/youtube_dl/options.py index 53497fbc6..f49f10873 100644 --- a/youtube_dl/options.py +++ b/youtube_dl/options.py @@ -756,6 +756,14 @@ def parseOpts(overrideArguments=None): '--recode-video', metavar='FORMAT', dest='recodevideo', default=None, help='Encode the video to another format if necessary (currently supported: mp4|flv|ogg|webm|mkv|avi)') + postproc.add_option( + '--slice-start', + metavar='START_TIME', dest='slicestart', default=None, + help='Slice start time') + postproc.add_option( + '--slice-end', + metavar='END_TIME', dest='sliceend', default=None, + help='Slice end time') postproc.add_option( '--postprocessor-args', dest='postprocessor_args', metavar='ARGS', diff --git a/youtube_dl/postprocessor/__init__.py b/youtube_dl/postprocessor/__init__.py index 3ea518399..bfd57db28 100644 --- a/youtube_dl/postprocessor/__init__.py +++ b/youtube_dl/postprocessor/__init__.py @@ -12,6 +12,7 @@ from .ffmpeg import ( FFmpegMetadataPP, FFmpegVideoConvertorPP, FFmpegSubtitlesConvertorPP, + FFmpegSlicePP, ) from .xattrpp import XAttrMetadataPP from .execafterdownload import ExecAfterDownloadPP @@ -33,6 +34,7 @@ __all__ = [ 'FFmpegMergerPP', 'FFmpegMetadataPP', 'FFmpegPostProcessor', + 'FFmpegSlicePP', 'FFmpegSubtitlesConvertorPP', 'FFmpegVideoConvertorPP', 'MetadataFromTitlePP', diff --git a/youtube_dl/postprocessor/ffmpeg.py b/youtube_dl/postprocessor/ffmpeg.py index 652b1cb53..b7eeef269 100644 --- a/youtube_dl/postprocessor/ffmpeg.py +++ b/youtube_dl/postprocessor/ffmpeg.py @@ -278,7 +278,7 @@ class FFmpegExtractAudioPP(FFmpegPostProcessor): prefix, sep, ext = path.rpartition('.') # not os.path.splitext, since the latter does not work on unicode in all setups new_path = prefix + sep + extension - + information['filepath'] = new_path information['ext'] = extension @@ -577,3 +577,25 @@ class FFmpegSubtitlesConvertorPP(FFmpegPostProcessor): } return sub_filenames, info + + +class FFmpegSlicePP(FFmpegPostProcessor): + + def __init__(self, downloader, slice_start_time, slice_end_time): + super(FFmpegSlicePP, self).__init__(downloader) + self.slice_start_time = slice_start_time + self.slice_end_time = slice_end_time + + def run(self, info): + filename = info['filepath'] + temp_filename = prepend_extension(filename, 'temp') + start_time = self.slice_start_time or "0" + end_time = self.slice_end_time or "{}".format(info['duration']) + options = ['-ss', start_time, '-to', end_time] + self.run_ffmpeg(filename, temp_filename, options) + msg = '[ffmpeg] Sliced: from ({}) seconds to ({}) seconds'.format( + start_time, end_time) + self._downloader.to_screen(msg) + os.remove(encodeFilename(filename)) + os.rename(encodeFilename(temp_filename), encodeFilename(filename)) + return [], info From bea82b3acf4570d41cb906ef6eee49a388b58ed2 Mon Sep 17 00:00:00 2001 From: marcwebbie Date: Sun, 16 Oct 2016 12:16:23 -0200 Subject: [PATCH 2/3] Fix python2.6 support with requested changes --- youtube_dl/options.py | 4 ++-- youtube_dl/postprocessor/ffmpeg.py | 16 ++++++++++++---- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/youtube_dl/options.py b/youtube_dl/options.py index f49f10873..12181772d 100644 --- a/youtube_dl/options.py +++ b/youtube_dl/options.py @@ -758,11 +758,11 @@ def parseOpts(overrideArguments=None): help='Encode the video to another format if necessary (currently supported: mp4|flv|ogg|webm|mkv|avi)') postproc.add_option( '--slice-start', - metavar='START_TIME', dest='slicestart', default=None, + metavar='TIME', dest='slicestart', default=None, help='Slice start time') postproc.add_option( '--slice-end', - metavar='END_TIME', dest='sliceend', default=None, + metavar='TIME', dest='sliceend', default=None, help='Slice end time') postproc.add_option( '--postprocessor-args', diff --git a/youtube_dl/postprocessor/ffmpeg.py b/youtube_dl/postprocessor/ffmpeg.py index b7eeef269..1682368fd 100644 --- a/youtube_dl/postprocessor/ffmpeg.py +++ b/youtube_dl/postprocessor/ffmpeg.py @@ -22,6 +22,7 @@ from ..utils import ( subtitles_filename, dfxp2srt, ISO639Utils, + parse_duration, ) @@ -586,15 +587,22 @@ class FFmpegSlicePP(FFmpegPostProcessor): self.slice_start_time = slice_start_time self.slice_end_time = slice_end_time + def _parse_time(self, duration): + start_time = self.slice_start_time or '0' + end_time = self.slice_end_time or ('%s' % duration) + start_time = "%s" % parse_duration(start_time) + end_time = "%s" % parse_duration(end_time) + return start_time, end_time + def run(self, info): + import pudb; pudb.set_trace() filename = info['filepath'] temp_filename = prepend_extension(filename, 'temp') - start_time = self.slice_start_time or "0" - end_time = self.slice_end_time or "{}".format(info['duration']) + start_time, end_time = self._parse_time(info['duration']) options = ['-ss', start_time, '-to', end_time] self.run_ffmpeg(filename, temp_filename, options) - msg = '[ffmpeg] Sliced: from ({}) seconds to ({}) seconds'.format( - start_time, end_time) + msg = ('[ffmpeg] Sliced: from "%s" seconds to "%s" seconds' % + (start_time, end_time)) self._downloader.to_screen(msg) os.remove(encodeFilename(filename)) os.rename(encodeFilename(temp_filename), encodeFilename(filename)) From b8c954c1f8c4d976388309cdba1ba57caf64ebe3 Mon Sep 17 00:00:00 2001 From: marcwebbie Date: Sun, 16 Oct 2016 12:36:07 -0200 Subject: [PATCH 3/3] Remove debugging line --- youtube_dl/postprocessor/ffmpeg.py | 1 - 1 file changed, 1 deletion(-) diff --git a/youtube_dl/postprocessor/ffmpeg.py b/youtube_dl/postprocessor/ffmpeg.py index 1682368fd..45ec13e67 100644 --- a/youtube_dl/postprocessor/ffmpeg.py +++ b/youtube_dl/postprocessor/ffmpeg.py @@ -595,7 +595,6 @@ class FFmpegSlicePP(FFmpegPostProcessor): return start_time, end_time def run(self, info): - import pudb; pudb.set_trace() filename = info['filepath'] temp_filename = prepend_extension(filename, 'temp') start_time, end_time = self._parse_time(info['duration'])