Reputation: 5531
I have written a small piece of code that downloads audios and videos from YouTube using YouTube-DL
. Here is my code:
from __future__ import unicode_literals
import youtube_dl
link = input("Enter the video link:")
while True:
choice = input("Enter a for audio file, v for video file:")
if choice == "a" or choice == "v":
break
ydl_opts = {}
if choice == "a":
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
else:
ydl_opts = {'format':137}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
info_dict = ydl.extract_info(link, download=False)
video_title = info_dict.get('title', None)
if choice == "a":
path = f'D:\\DwnldsYT\\{video_title}.mp3'
if choice == "v":
path = f'D:\\DwnldsYT\\{video_title}.mp4'
ydl_opts.update({'outtmpl':path})
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([link])
This works fine. I am able to play the downloaded audio. But when I try to read the same audio file, it shows me an error.
Code:
from pydub import AudioSegment
song = AudioSegment.from_mp3("D:\\DwnldsYT\\Cartoon - On & On (feat. Daniel Levi) [NCS Release].mp3")
Output:
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[abuffer @ 000001f242280300] Value inf for parameter 'time_base' out of range [0 - 2.14748e+09]
Last message repeated 3 times
[abuffer @ 000001f242280300] Error setting option time_base to value 1/0.
[graph_0_in_0_0 @ 000001f242280e80] Error applying options to the filter.
Error reinitializing filters!
Error while filtering: Result too large
Finishing stream 0:0 without any data written to it.
[abuffer @ 000001f242280300] Value inf for parameter 'time_base' out of range [0 - 2.14748e+09]
Last message repeated 3 times
[abuffer @ 000001f242280300] Error setting option time_base to value 1/0.
[graph_0_in_0_0 @ 000001f242280e80] Error applying options to the filter.
Error configuring filter graph
Conversion failed!
Is this because the audio downloaded is corrupted? And how to solve this problem? Any help would be appreciated. Thanks!
Upvotes: 2
Views: 4452
Reputation: 16747
Your audio file is probably encoded not as MP3. It is probably AAC (usually having file extension .aac
), which is the default format for .mp4
and AVC video codec and youtube.
Instead of AudioSegment.from_mp3(path)
try doing AudioSegment.from_file(path)
it will detect format by itself or AudioSegment.from_file(path, format = 'aac')
to force it to decode AAC.
Also you can detect format by using FFMpeg by running ffmpeg -i filename.mp3
, for MP3 it should output something like Stream #0:0: Audio: mp3, 44100 Hz, stereo, fltp, 320 kb/s
, and for other formats it will be non-mp3
string.
Probably youtube-dl
library has some options to always force re-encoding to MP3, if you really need only MP3, although beware that you will lose some quality by re-conversion. Right now your options contain preferred
word for MP3, it means that it prefers MP3 downloading, but doesn't force this output format.
If it doesn't matter for you if it is MP3 or AAC, then stay with original youtube's format, you will keep best quality then, you may also rename files with .aac
extension for clarity.
If you want Youtube-downloaded files to be processed later e.g. by 8D Audio Site, then not to lose quality when processing you need to convert your file to some lossless format like WAV. You can do this using FFMpeg tool, like this ffmpeg -i filename.aac filename.wav
.
Also 8D Audio Site has 20MB
file size limit, hence you may need to split your WAV file. In order to split into parts of 30 seconds each you need to run ffmpeg -i filename.wav -f segment -segment_time 30 -c copy "filename%03d.wav"
and then upload and process each part. You can concatenate processed WAVs afterwards using sox as sox sound0*.wav soundall.wav
. And convert to some compressed but lossless format like FLAC as ffmpeg -i soundall.wav soundall.flac
.
All conversion/splitting/merging commands can be easilty automated using Python's standard module subprocess. E.g. to run any command like ffmpeg -i sound.mp3 sound.wav
you have just to do in Python one line subprocess.run(['ffmpeg', '-i', 'sound.mp3', 'sound.wav'], check = True)
.
All professionals are editing and storing audio files in some lossless format like WAV or FLAC, not to lose quality on each stage of processing, so it is a good practice, especially if you want to 8D-process file definitely MP3 reconversion several times will be a bad idea.
If this library does 8D conversion mathematically correctly then my next code will do correct WAV conversion, it is just a minimal working sub-code of that library for WAV only.
# Needs: python -m pip install pydub numpy
def ConvertTo8D(input_file_name, output_file_name, *, period = 200):
import numpy as np
from pydub import AudioSegment
if period < 0:
period = -period
elif period == 0:
period = 200
audio = AudioSegment.from_file(input_file_name)
audio = audio + AudioSegment.silent(duration = 150)
eightD = AudioSegment.empty()
pan = 0.9 * np.sin(np.linspace(0, 2. * np.pi, period))
for i, chunk in enumerate(audio[::100]):
if len(chunk) < 100:
continue
newChunk = chunk.pan(pan[i % period])
eightD = eightD + newChunk
eightD.export(output_file_name, format = output_file_name[output_file_name.rfind('.') + 1:])
# Test Usage
ConvertTo8D('sound.wav', 'sound-8D.wav')
Upvotes: 3