I am in the process of writing a python program that downloads music from youtube via a search query in mp3 format.
import youtube_dl
import urllib.request
import urllib.parse
import re
ydlOpts = {
'format': 'best',
'extractaudio': True,
'audioformat': 'best',
'noplaylist': True,
'outtmpl': '/path/to/folder/%(title)s.%(ext)s',
'postprocessors': [{
'preferredconc': 'mp3',
'preferredquality': 192
}]
}
def query(search):
queryString = urllib.parse.urlencode({'search_query': search})
htmlContent = urllib.request.urlopen("http://www.youtube.com/results?" + queryString)
searchResults = re.findall(r'href=\"\/watch\?v=(.{11})', htmlContent.read().decode())
url = "http://www.youtube.com/watch?v=" + searchResults[0]
print (url)
return url
searchList = ['Alone Together Paul Desmond', 'Agua De Beber Astrud Gilberto', 'Molasses Hiatus Kaiyote']
youtubeList = []
for i in searchList:
youtubeList.append(query(i))
youtube_dl.YoutubeDL(ydlOpts).download([youtubeList[0]])`
However, when trying to run this code that tests out the code with just the first search query using avconv for postprocessing, I get an error regarding the 'key' value under 'postprocessors' in the youtube-dl options. I initially omitted it from postprocessors as youtube-dl should automatically use avconv for processing, and all examples for audio extraction in the youtube-dl documentation use ffmpegExtractAudio instead: 'key': 'ffmpegExtractAudio'
However, omitting the key throws this error:
Traceback (most recent call last): File "downloadmusic.py", line 36, in youtube_dl.YoutubeDL(ydlOpts).download([youtubeList[0]]) File "/Users/Yo/anaconda3/lib/python3.6/site-packages/youtube_dl/YoutubeDL.py", line 417, in init pp_class = get_postprocessor(pp_def_raw['key']) KeyError: 'key'
I could not find any documentation for the key value for avconv instead of ffmpeg processing, and changing the value of 'audioformat' in ydlOpts to 'mp3' still results in a .webm file to be downloaded instead, hence the need for post-processing.
Using youtube-dl as a commandline tool this way works:
youtube-dl -x --audio-format mp3 --audio-quality 0 -o '/path/to/folder/%(title)s.%(ext)s' URL
What should the key value be for avconv post-processing? And why doesn't changing 'audioformat' to 'mp3' work?
Thank you!