How to download youtube videos as a best quality audio mp3 using youtube-dl

From man youtube-dl:

-x, --extract-audio                  Convert video files to audio-only files (requires ffmpeg or avconv and ffprobe or avprobe)
    --audio-format FORMAT            Specify audio format: "best", "aac", "vorbis", "mp3", "m4a", "opus", or "wav"; "best" by default
    --audio-quality QUALITY          Specify ffmpeg/avconv audio quality, insert a value between 0 (better) and 9 (worse) for VBR or a specific bitrate like 128K (default

So your command could be:

youtube-dl -f bestaudio --extract-audio --audio-format mp3 --audio-quality 0 <Video-URL>

The --audio-quality 0 uses the highest encoding quality but can increase your file size unnecessarily. The default is 5 and might be a better choice depending on the source quality.

So, if quality and file size matter to you, you should avoid re-encoding and stay with Youtube's native music formats:

youtube-dl -f bestaudio[ext=m4a] --embed-thumbnail --add-metadata <Video-URL>

resulting in an m4a file or

youtube-dl -f bestaudio --extract-audio --embed-thumbnail --add-metadata <Video-URL>

probably resulting in an ogg file.

You can list the available format with

youtube-dl -F <Video-URL>

If you only want the mp3 file:

youtube-dl -f bestaudio  "https://www.youtube.com/playlist?list=PLYRruMbyFRcBVdVN8v4FNkIKkXvL-bZn_" --exec "ffmpeg -i {}  -codec:a libmp3lame -qscale:a 0 {}.mp3 && rm {} "

If you do not want to remove the original file:

youtube-dl -f bestaudio  "https://www.youtube.com/playlist?list=PLYRruMbyFRcBVdVN8v4FNkIKkXvL-bZn_" --exec "ffmpeg -i {}  -codec:a libmp3lame -qscale:a 0 {}.mp3 "

I recommend use Pafy (Python), very easy to get audio link, and you can download directly if you want:

  • https://pypi.python.org/pypi/pafy
  • http://pythonhosted.org/pafy/
#python3
import pafy
video = pafy.new('video id or video url')
bestaudio = video.getbestaudio()
bestaudio.bitrate #get bit rate
bestaudio.extension #extension of audio fileurl
...
bestaudio.url #get url
...
#download if you want
bestaudio.download()