From b3f258ca57369ceae6227eaa7e721bb09d83282e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaime=20Marqui=CC=81nez=20Ferra=CC=81ndiz?= Date: Mon, 22 Jul 2013 22:57:21 +0200 Subject: [PATCH] Make the join videos class behave like a normal PostProcessor, executed if the "--join-parts" is given --- youtube_dl/PostProcessor.py | 23 ++++++++++++++++------- youtube_dl/YoutubeDL.py | 13 ------------- youtube_dl/__init__.py | 5 +++++ 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/youtube_dl/PostProcessor.py b/youtube_dl/PostProcessor.py index 9caed69a1..75b4f2cb5 100644 --- a/youtube_dl/PostProcessor.py +++ b/youtube_dl/PostProcessor.py @@ -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) diff --git a/youtube_dl/YoutubeDL.py b/youtube_dl/YoutubeDL.py index 3e4818570..0500bb146 100644 --- a/youtube_dl/YoutubeDL.py +++ b/youtube_dl/YoutubeDL.py @@ -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 diff --git a/youtube_dl/__init__.py b/youtube_dl/__init__.py index 102508cf9..30ce9d035 100644 --- a/youtube_dl/__init__.py +++ b/youtube_dl/__init__.py @@ -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())