From b0a945e2d2a000a077077c0bf58cdf7b763d63d1 Mon Sep 17 00:00:00 2001 From: Mello-Yello Date: Sat, 28 Jul 2018 11:01:49 +0200 Subject: [PATCH] Add the --trim-silence parameter --- core/convert.py | 17 ++++++++++++----- core/handle.py | 5 +++++ spotdl.py | 2 +- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/core/convert.py b/core/convert.py index 19ace4e..7fa547b 100644 --- a/core/convert.py +++ b/core/convert.py @@ -15,11 +15,11 @@ https://trac.ffmpeg.org/wiki/Encode/AAC """ -def song(input_song, output_song, folder, avconv=False): +def song(input_song, output_song, folder, avconv=False, trim_silence=True): """ Do the audio format conversion. """ if input_song == output_song: return 0 - convert = Converter(input_song, output_song, folder) + convert = Converter(input_song, output_song, folder, trim_silence) log.info('Converting {0} to {1}'.format( input_song, output_song.split('.')[-1])) if avconv: @@ -30,9 +30,10 @@ def song(input_song, output_song, folder, avconv=False): class Converter: - def __init__(self, input_song, output_song, folder): + def __init__(self, input_song, output_song, folder, trim_silence): self.input_file = os.path.join(folder, input_song) self.output_file = os.path.join(folder, output_song) + self.trim_silence = trim_silence def with_avconv(self): if log.level == 10: @@ -43,7 +44,10 @@ class Converter: command = ['avconv', '-loglevel', level, '-i', self.input_file, '-ab', '192k', self.output_file, '-y'] - + + if self.trim_silence: + log.warning('--trim-silence not supported with avconv') + log.debug(command) return subprocess.call(command) @@ -75,8 +79,11 @@ class Converter: # add common params for any of the above combination ffmpeg_params += '-b:a 192k -vn ' - ffmpeg_params += '-af silenceremove=start_periods=1 ' ffmpeg_pre += ' -i' + + if self.trim_silence: + ffmpeg_params += '-af silenceremove=start_periods=1 ' + command = ffmpeg_pre.split() + [self.input_file] + ffmpeg_params.split() + [self.output_file] log.debug(command) diff --git a/core/handle.py b/core/handle.py index f8f7e9f..b2146d0 100644 --- a/core/handle.py +++ b/core/handle.py @@ -20,6 +20,7 @@ default_conf = { 'spotify-downloader': 'overwrite' : 'prompt', 'input-ext' : '.m4a', 'output-ext' : '.mp3', + 'trim-silence' : False, 'download-only-metadata' : False, 'dry-run' : False, 'music-videos-only' : False, @@ -153,6 +154,10 @@ def get_arguments(raw_args=None, to_group=True, to_merge=True): help='file format to save the downloaded track with, each tag ' 'is surrounded by curly braces. Possible formats: ' '{}'.format([internals.formats[x] for x in internals.formats])) + parser.add_argument( + '--trim-silence', default=config['trim-silence'], + help='remove silence from the start of the audio', + action='store_true') parser.add_argument( '-sf', '--search-format', default=config['search-format'], help='search format to search for on YouTube, each tag ' diff --git a/spotdl.py b/spotdl.py index bfd1e26..2d0dcc5 100755 --- a/spotdl.py +++ b/spotdl.py @@ -159,7 +159,7 @@ def download_single(raw_song, number=None): print('') try: convert.song(input_song, output_song, const.args.folder, - avconv=const.args.avconv) + avconv=const.args.avconv, trim_silence=const.args.trim_silence) except FileNotFoundError: encoder = 'avconv' if const.args.avconv else 'ffmpeg' log.warning('Could not find {0}, skipping conversion'.format(encoder))