diff --git a/youtube_dl/__init__.py b/youtube_dl/__init__.py index 9bb952457..3e4ecc2f1 100644 --- a/youtube_dl/__init__.py +++ b/youtube_dl/__init__.py @@ -283,6 +283,13 @@ def _real_main(argv=None): postprocessors.append({ 'key': 'FFmpegEmbedSubtitle', }) + if opts.starttime or opts.endtime: + d = {'key': 'FFmpegCutVideo'} + if opts.starttime: + d['startTime'] = int(opts.starttime) + if opts.endtime: + d['endTime'] = int(opts.endtime) + postprocessors.append(d) if opts.embedthumbnail: already_have_thumbnail = opts.writethumbnail or opts.write_all_thumbnails postprocessors.append({ diff --git a/youtube_dl/options.py b/youtube_dl/options.py index 7d1bbc021..6bf0ea197 100644 --- a/youtube_dl/options.py +++ b/youtube_dl/options.py @@ -853,6 +853,14 @@ def parseOpts(overrideArguments=None): '--convert-subs', '--convert-subtitles', metavar='FORMAT', dest='convertsubtitles', default=None, help='Convert the subtitles to other format (currently supported: srt|ass|vtt|lrc)') + postproc.add_option( + '--start-time', + metavar='TIME', dest='starttime', default=None, + help='Cuts the video/audio starting at start-time') + postproc.add_option( + '--end-time', + metavar='TIME', dest='endtime', default=None, + help='Cuts the vidoe/audio up to end-time') parser.add_option_group(general) parser.add_option_group(network) diff --git a/youtube_dl/postprocessor/ffmpeg.py b/youtube_dl/postprocessor/ffmpeg.py index 973d9ad84..b5bae917f 100644 --- a/youtube_dl/postprocessor/ffmpeg.py +++ b/youtube_dl/postprocessor/ffmpeg.py @@ -227,32 +227,38 @@ class FFmpegCutVideoPP(FFmpegPostProcessor): return "%d:%02d:%02d" % (h, m, s) def run(self, information): - if not self._startTime and not self._endTime: - self._downloader.to_screen('[ffmpeg] No startTime or endTime. Keeping original') + if self._startTime == 0 and self._endTime == None: + self._downloader.to_screen('[ffmpeg] No start time or end time. Keeping original') return [], information - if self._endTime <= self._startTime: - raise PostProcessingError("WARNING: endTime smaller than startTime") + if self._endTime and self._endTime <= self._startTime: + raise PostProcessingError("end time smaller or equal to the start time") - duration = information['duration'] - if self._endTime and self._endTime > duration: - self._downloader.to_screen('WARNING: endTime greater than video duration') + if self._endTime == 0: + raise PostProcessingError("end time can't be zero") + + duration = information.get('duration') + + if self._endTime and duration and self._endTime >= duration: + self._downloader.to_screen('WARNING: end time greater than video duration') self._endTime = None options = ['-c', 'copy'] - message = '[ffmpeg] Cutting video ' + message = '[ffmpeg] Cutting video ' if self._startTime: start = self.toTime(self._startTime) options.extend(['-ss', start]) message += 'from %s ' % (start) - duration -= self._startTime + if duration: + duration -= self._startTime - if self._endTime and self._endTime < duration: + if self._endTime: end = self.toTime(self._endTime) options.extend(['-to', end]) message += 'to %s' % (end) - duration -= self._endTime + if duration: + duration -= self._endTime if '-to' not in options and '-ss' not in options: self._downloader.to_screen('[ffmpeg] Nothing to cut. Keeping original') @@ -261,7 +267,8 @@ class FFmpegCutVideoPP(FFmpegPostProcessor): path = information['filepath'] temp_filename = prepend_extension(path, 'temp') self._downloader.to_screen(message) - information['duration'] = duration + if duration: + information['duration'] = duration self.run_ffmpeg(path, temp_filename, options) os.remove(encodeFilename(path)) os.rename(encodeFilename(temp_filename), encodeFilename(path))