mirror of
https://github.com/l1ving/youtube-dl
synced 2025-03-14 07:37:25 +08:00
Make the join videos class behave like a normal PostProcessor, executed if the "--join-parts" is given
This commit is contained in:
parent
df05dc0ce6
commit
b3f258ca57
@ -512,16 +512,25 @@ class FFmpegMetadataPP(FFmpegPostProcessor):
|
||||
return True, info
|
||||
|
||||
|
||||
class FFmpegJoinVideos(FFmpegPostProcessor):
|
||||
def join(self, final_video, videos):
|
||||
files_file = u'%s.videos' % final_video
|
||||
class FFmpegJoinVideosPP(FFmpegPostProcessor):
|
||||
def run(self, information):
|
||||
filename = information['filepath']
|
||||
parts = information.get('parts')
|
||||
if parts is None or len(parts) == 1:
|
||||
return (True, information)
|
||||
parts_files = [u'%s.%s' % (filename, i) for (i, _) in enumerate(parts)]
|
||||
files_file = u'%s.videos' % filename
|
||||
with io.open(encodeFilename(files_file), 'w', encoding='utf-8') as f:
|
||||
for video in videos:
|
||||
for video in parts_files:
|
||||
f.write(u'file \'%s\'\n' % video)
|
||||
self._downloader.to_screen(u'[ffmpeg] Joining video parts, destination: %s' % final_video)
|
||||
self._downloader.to_screen(u'[ffmpeg] Joining video parts, destination: %s' % filename)
|
||||
try:
|
||||
self.run_ffmpeg(files_file, final_video, ['-c', 'copy'], ['-f', 'concat'])
|
||||
self.run_ffmpeg(files_file, filename, ['-c', 'copy'], ['-f', 'concat'])
|
||||
except FFmpegPostProcessorError:
|
||||
return False
|
||||
os.remove(encodeFilename(files_file))
|
||||
return True
|
||||
# We have to manually remove the parts if requested
|
||||
if not self._downloader.params.get('keepvideo', False):
|
||||
for part_file in parts_files:
|
||||
os.remove(encodeFilename(part_file))
|
||||
return (True, information)
|
||||
|
@ -52,7 +52,6 @@ from .utils import (
|
||||
from .extractor import get_info_extractor, gen_extractors
|
||||
from .FileDownloader import FileDownloader
|
||||
from .version import __version__
|
||||
from .PostProcessor import FFmpegJoinVideos
|
||||
|
||||
|
||||
class YoutubeDL(object):
|
||||
@ -787,25 +786,13 @@ class YoutubeDL(object):
|
||||
success = True
|
||||
else:
|
||||
parts_success = []
|
||||
parts_files = []
|
||||
self.to_screen(u'[info] Downloading %s parts' % len(parts))
|
||||
for (i, part) in enumerate(parts):
|
||||
part_info = dict(info_dict)
|
||||
part_info.update(part)
|
||||
part_filename = u'%s.%s' % (filename, i)
|
||||
parts_files.append(part_filename)
|
||||
parts_success.append(self.fd._do_download(part_filename, part_info))
|
||||
success = all(parts_success)
|
||||
if success:
|
||||
video_joiner = FFmpegJoinVideos(self)
|
||||
join_success = video_joiner.join(filename, parts_files)
|
||||
if not join_success:
|
||||
self.report_error(u'Could not join the video parts')
|
||||
else:
|
||||
self.to_screen(u'[info] Removing video parts')
|
||||
for part_file in parts_files:
|
||||
os.remove(encodeFilename(part_file))
|
||||
success = join_success
|
||||
except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
|
||||
self.report_error(u'unable to download video data: %s' % str(err))
|
||||
return
|
||||
|
@ -76,6 +76,7 @@ from .PostProcessor import (
|
||||
FFmpegVideoConvertor,
|
||||
FFmpegExtractAudioPP,
|
||||
FFmpegEmbedSubtitlePP,
|
||||
FFmpegJoinVideosPP,
|
||||
)
|
||||
|
||||
|
||||
@ -390,6 +391,8 @@ def parseOpts(overrideArguments=None):
|
||||
help='ffmpeg/avconv audio quality specification, insert a value between 0 (better) and 9 (worse) for VBR or a specific bitrate like 128K (default 5)')
|
||||
postproc.add_option('--recode-video', metavar='FORMAT', dest='recodevideo', default=None,
|
||||
help='Encode the video to another format if necessary (currently supported: mp4|flv|ogg|webm)')
|
||||
postproc.add_option('--join-parts', action='store_true', dest='joinparts', default=False,
|
||||
help='Join the video parts if the video is splitted in different parts.')
|
||||
postproc.add_option('-k', '--keep-video', action='store_true', dest='keepvideo', default=False,
|
||||
help='keeps the video file on disk after the post-processing; the video is erased by default')
|
||||
postproc.add_option('--no-post-overwrites', action='store_true', dest='nopostoverwrites', default=False,
|
||||
@ -658,6 +661,8 @@ def _real_main(argv=None):
|
||||
ydl.add_default_info_extractors()
|
||||
|
||||
# PostProcessors
|
||||
if opts.joinparts:
|
||||
ydl.add_post_processor(FFmpegJoinVideosPP())
|
||||
# Add the metadata pp first, the other pps will copy it
|
||||
if opts.addmetadata:
|
||||
ydl.add_post_processor(FFmpegMetadataPP())
|
||||
|
Loading…
x
Reference in New Issue
Block a user