This commit is contained in:
Marcos Alfredo Núñez 2020-09-26 11:07:08 +10:00 committed by GitHub
commit 989cc7489f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 76 additions and 0 deletions

View File

@ -285,6 +285,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({

View File

@ -858,6 +858,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)

View File

@ -3,6 +3,7 @@ from __future__ import unicode_literals
from .embedthumbnail import EmbedThumbnailPP
from .ffmpeg import (
FFmpegPostProcessor,
FFmpegCutVideoPP,
FFmpegEmbedSubtitlePP,
FFmpegExtractAudioPP,
FFmpegFixupStretchedPP,
@ -26,6 +27,7 @@ __all__ = [
'EmbedThumbnailPP',
'ExecAfterDownloadPP',
'FFmpegEmbedSubtitlePP',
'FFmpegCutVideoPP',
'FFmpegExtractAudioPP',
'FFmpegFixupM3u8PP',
'FFmpegFixupM4aPP',

View File

@ -246,6 +246,65 @@ class FFmpegPostProcessor(PostProcessor):
return 'file:' + fn if fn != '-' else fn
class FFmpegCutVideoPP(FFmpegPostProcessor):
def __init__(self, downloader=None, startTime=0, endTime=None):
FFmpegPostProcessor.__init__(self, downloader)
self._startTime = startTime
self._endTime = endTime
if self._endTime and self._endTime <= self._startTime:
raise PostProcessingError("end time smaller or equal to the start time")
if self._endTime == 0:
raise PostProcessingError("end time can't be zero")
def toTime(self, seconds):
m, s = divmod(seconds, 60)
h, m = divmod(m, 60)
return "%d:%02d:%02d" % (h, m, s)
def run(self, information):
if self._startTime == 0 and self._endTime == None:
self._downloader.to_screen('[ffmpeg] No start time or end time. Keeping original')
return [], information
duration = information.get('duration')
if self._endTime and duration and self._endTime >= duration:
self._downloader.to_screen('WARNING: end time greater or equal to duration')
self._endTime = None
options = ['-c', 'copy']
message = '[ffmpeg] Cutting video '
if self._startTime:
start = self.toTime(self._startTime)
options.extend(['-ss', start])
message += 'from %s ' % (start)
if duration:
duration -= self._startTime
if self._endTime:
end = self.toTime(self._endTime)
options.extend(['-to', end])
message += 'to %s' % (end)
if duration:
duration = self._endTime - (duration - self._endTime)
if '-to' not in options and '-ss' not in options:
self._downloader.to_screen('[ffmpeg] Nothing to cut. Keeping original')
return [], information
path = information['filepath']
temp_filename = prepend_extension(path, 'temp')
self._downloader.to_screen(message)
if duration:
information['duration'] = duration
self.run_ffmpeg(path, temp_filename, options)
os.remove(encodeFilename(path))
os.rename(encodeFilename(temp_filename), encodeFilename(path))
return [], information
class FFmpegExtractAudioPP(FFmpegPostProcessor):
def __init__(self, downloader=None, preferredcodec=None, preferredquality=None, nopostoverwrites=False):
FFmpegPostProcessor.__init__(self, downloader)