From a12164119d944baa8307dbfc75c6ab50f29729f4 Mon Sep 17 00:00:00 2001 From: SFMDI <36741818+SFMDI@users.noreply.github.com> Date: Sat, 14 Dec 2019 16:01:30 +0900 Subject: [PATCH] add new option audio extraction add option -e you can extract audio form downloaded video --- src/you_get/common.py | 33 +++++++++++++++++++++++++++++++-- src/you_get/processor/ffmpeg.py | 24 +++++++++++++++++++++++- 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/src/you_get/common.py b/src/you_get/common.py index 2397a0a6..1ccee11e 100755 --- a/src/you_get/common.py +++ b/src/you_get/common.py @@ -138,6 +138,8 @@ cookies = None output_filename = None auto_rename = False insecure = False +extract_audio = False +extract_format = 'mp3' fake_headers = { 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', # noqa @@ -981,6 +983,9 @@ def download_urls( headers=headers, **kwargs ) bar.done() + if extract_audio: + from .processor.ffmpeg import ffmpeg_extract_audio_from_video + ffmpeg_extract_audio_from_video(output_filepath, ext=extract_format) else: parts = [] print('Downloading %s ...' % tr(output_filename)) @@ -1007,6 +1012,9 @@ def download_urls( from .processor.ffmpeg import ffmpeg_concat_av ret = ffmpeg_concat_av(parts, output_filepath, ext) print('Merged into %s' % output_filename) + if extract_audio: + from .processor.ffmpeg import ffmpeg_extract_audio_from_video + ffmpeg_extract_audio_from_video(output_filepath, ext=extract_format) if ret == 0: for part in parts: os.remove(part) @@ -1021,6 +1029,9 @@ def download_urls( from .processor.join_flv import concat_flv concat_flv(parts, output_filepath) print('Merged into %s' % output_filename) + if extract_audio: + from .processor.ffmpeg import ffmpeg_extract_audio_from_video + ffmpeg_extract_audio_from_video(output_filepath, ext=extract_format) except: raise else: @@ -1032,11 +1043,13 @@ def download_urls( from .processor.ffmpeg import has_ffmpeg_installed if has_ffmpeg_installed(): from .processor.ffmpeg import ffmpeg_concat_mp4_to_mp4 - ffmpeg_concat_mp4_to_mp4(parts, output_filepath) else: from .processor.join_mp4 import concat_mp4 concat_mp4(parts, output_filepath) print('Merged into %s' % output_filename) + if extract_audio: + from .processor.ffmpeg import ffmpeg_extract_audio_from_video + ffmpeg_extract_audio_from_video(output_filepath, ext=extract_format) except: raise else: @@ -1053,6 +1066,9 @@ def download_urls( from .processor.join_ts import concat_ts concat_ts(parts, output_filepath) print('Merged into %s' % output_filename) + if extract_audio: + from .processor.ffmpeg import ffmpeg_extract_audio_from_video + ffmpeg_extract_audio_from_video(output_filepath, ext=extract_format) except: raise else: @@ -1520,11 +1536,18 @@ def script_main(download, download_playlist, **kwargs): '-a', '--auto-rename', action='store_true', default=False, help='Auto rename same name different files' ) - download_grp.add_argument( '-k', '--insecure', action='store_true', default=False, help='ignore ssl errors' ) + download_grp.add_argument( + '-e', '--extract-audio', action='store_true', default=False, + help='extract audio from video' + ) + download_grp.add_argument( + '-E', '--extract-audio-format', metavar='FORMAT', default='mp3', + help='set extracted audio format' + ) proxy_grp = parser.add_argument_group('Proxy options') proxy_grp = proxy_grp.add_mutually_exclusive_group() @@ -1572,6 +1595,9 @@ def script_main(download, download_playlist, **kwargs): global output_filename global auto_rename global insecure + global extract_audio + global extract_format + output_filename = args.output_filename extractor_proxy = args.extractor_proxy @@ -1600,6 +1626,9 @@ def script_main(download, download_playlist, **kwargs): if args.player: player = args.player caption = False + if args.extract_audio: + extract_audio = True + extract_format = args.extract_audio_format if args.insecure: # ignore ssl diff --git a/src/you_get/processor/ffmpeg.py b/src/you_get/processor/ffmpeg.py index 63679b83..70c48368 100755 --- a/src/you_get/processor/ffmpeg.py +++ b/src/you_get/processor/ffmpeg.py @@ -264,7 +264,7 @@ def ffmpeg_download_stream(files, title, ext, params={}, output_dir='.', stream= def ffmpeg_concat_audio_and_video(files, output, ext): print('Merging video and audio parts... ', end="", flush=True) - if has_ffmpeg_installed: + if has_ffmpeg_installed(): params = [FFMPEG] + LOGLEVEL params.extend(['-f', 'concat']) params.extend(['-safe', '0']) # https://stackoverflow.com/questions/38996925/ffmpeg-concat-unsafe-file-name @@ -288,3 +288,25 @@ def ffprobe_get_media_duration(file): params.extend(['-v', 'quiet']) params.extend(['-of', 'csv=p=0']) return subprocess.check_output(params, stdin=STDIN, stderr=subprocess.STDOUT).decode().strip() + + +def ffmpeg_extract_audio_from_video(video_file_path, ext='mp3'): + if has_ffmpeg_installed(): + print('extracting audio from video... ', end="", flush=True) + + path_without_ext = os.path.splitext(video_file_path)[0] + output_path = path_without_ext+"."+ ext + + params = [FFMPEG] + LOGLEVEL + params.extend(['-i', video_file_path]) + params.extend(['-f', ext]) + params.append(path_without_ext+"."+ext) + # os.remove(video_file_path) + return_val = subprocess.call(params, stdin=STDIN) + if return_val == 0: + print('\nextracted into ' + output_path) + else: + print("\nextract error") + return + else: + raise EnvironmentError('\nNo ffmpeg found') \ No newline at end of file