Skip unnecessary MP3 conversion via FFmpeg after audio download

Currently, after downloading YouTube videos, FFmpeg is used to convert
the downloaded audio file into MP3 for historic reasons (there was no
database to keep the metadata around, so the ID3 tags in the MP3 file
were necessary to be able to read the metadata later on).

Now that a metadata database exists, this is no longer necessary. This
conversion is also fairly straining for slower CPUs or CPUs that do not
offer the appropriate processor extensions to be able to accelerate this
conversion, such as older ARM devices - in my case an ARMv7 32-bit
device, where the conversion could take over a minute for a fairly
simple 3-minute audio file of keeping a single core maxed out.

This should also result in less latency playing audio files on stronger
processors, though probably less noticeably.

Fixes #205
This commit is contained in:
Gert 2020-09-20 14:31:27 +02:00 committed by Terry Geng
parent 291b79ae7e
commit 47687a5e06

View File

@ -45,7 +45,7 @@ class URLItem(BaseItem):
self.title = "" self.title = ""
self.duration = 0 self.duration = 0
self.id = hashlib.md5(url.encode()).hexdigest() self.id = hashlib.md5(url.encode()).hexdigest()
self.path = var.tmp_folder + self.id + ".mp3" self.path = var.tmp_folder + self.id
self.thumbnail = "" self.thumbnail = ""
self.keywords = "" self.keywords = ""
else: else:
@ -145,8 +145,7 @@ class URLItem(BaseItem):
self.downloading = True self.downloading = True
base_path = var.tmp_folder + self.id base_path = var.tmp_folder + self.id
save_path = base_path + ".%(ext)s" save_path = base_path
mp3_path = base_path + ".mp3"
# Download only if music is not existed # Download only if music is not existed
self.ready = "preparing" self.ready = "preparing"
@ -154,15 +153,10 @@ class URLItem(BaseItem):
self.log.info("bot: downloading url (%s) %s " % (self.title, self.url)) self.log.info("bot: downloading url (%s) %s " % (self.title, self.url))
ydl_opts = { ydl_opts = {
'format': 'bestaudio/best', 'format': 'bestaudio/best',
'outtmpl': save_path, 'outtmpl': base_path,
'noplaylist': True, 'noplaylist': True,
'writethumbnail': True, 'writethumbnail': True,
'updatetime': False, 'updatetime': False
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192'},
{'key': 'FFmpegMetadata'}]
} }
with youtube_dl.YoutubeDL(ydl_opts) as ydl: with youtube_dl.YoutubeDL(ydl_opts) as ydl:
@ -180,7 +174,7 @@ class URLItem(BaseItem):
self.log.error("bot: download failed with error:\n %s" % error) self.log.error("bot: download failed with error:\n %s" % error)
if download_succeed: if download_succeed:
self.path = mp3_path self.path = save_path
self.ready = "yes" self.ready = "yes"
self.log.info( self.log.info(
"bot: finished downloading url (%s) %s, saved to %s." % (self.title, self.url, self.path)) "bot: finished downloading url (%s) %s, saved to %s." % (self.title, self.url, self.path))