How to download youtube videos as a best quality audio mp3 using youtube-dl [duplicate]
I want to download YouTube videos as mp3 files in its best quality. The below code helps to download youtube videos as m4a
but not us mp3
youtube-dl -f bestaudio --audio-quality 0 --audio-format mp3 https://www.youtube.com/playlist?list=PLYRruMbyFRcBVdVN8v4FNkIKkXvL-bZn_
can you tell me how to use bestaudio
parameter to download YouTube video as mp3
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()
It is also worth noting other pieces out of man youtube-dl
(a online copy can be found here):
(For those thinking TL;DR, just read the 4th paragraph, and you can use youtube-dl -x URL
as it should download the best quality anyway :)
Also youtube-dl -f bestaudio URL
works in my experience! However with both these options most of the time you will need convert to MP3 using ffmpeg
or similar - since you are already editing the file you can also tag it (use the old ID3 version for most device compatibility!).
FORMAT SELECTION
By default youtube-dl tries to download the best quality, but sometimes you may want to download other format. The simplest case is requesting a specific format, for example
-f 22
. You can get the list of available formats using--list-formats
, you can also use a file extension (currently it supports aac, m4a, mp3, mp4, ogg, wav, webm) or the special names best, bestvideo, bestaudio and worst.If you want to download multiple videos and they don't have the same formats available, you can specify the order of preference using slashes, as in
-f 22/17/18
. You can also filter the video results by putting a condition in brackets, as in-f "best[height=720]"
(or-f "[filesize>10M]"
). This works for filesize, height, width, tbr, abr, vbr, asr, and fps and the comparisons <, <=, >, >=, =, != and for ext, codec, vcodec, container, and protocol and the comparisons =, != . Formats for which the value is not known are excluded unless you put a question mark (?) after the operator. You can combine format filters, so-f "[height <=? 720][tbr>500]"
selects up to 720p videos (or videos where the height is not known) with a bitrate of at least 500 KBit/s.Use commas to download multiple formats, such as
-f 136/137/mp4/bestvideo,140/m4a/bestaudio.
You can merge the video and audio of two formats into a single file using-f <video-format>+<audio-format>
(requires ffmpeg or avconv), for example-f bestvideo+bestaudio.
Since the end of April 2015 and version 2015.04.26 youtube-dl uses
-f bestvideo+bestaudio/best
as default format selection (see #5447, #5456). If ffmpeg or avconv are installed this results in downloading bestvideo and bestaudio separately and muxing them together into a single file giving the best overall quality available. Otherwise it falls back to best and results in downloading best available quality served as a single file. best is also needed for videos that don't come from YouTube because they don't provide the audio and video in two different files. If you want to only download some dash formats (for example if you are not interested in getting videos with a resolution higher than 1080p), you can add-f bestvideo[height<=?1080]+bestaudio/best
to your configuration file. Note that if you use youtube-dl to stream to stdout (and most likely to pipe it to your media player then), i.e. you explicitly specify output template as-o -
, youtube-dl still uses-f best
format selection in order to start content delivery immediately to your player and not to wait until bestvideo and bestaudio are downloaded and muxed.If you want to preserve the old format selection behavior (prior to youtube-dl 2015.04.26), i.e. you want to download best available quality media served as a single file, you should explicitly specify your choice with
-f best
. You may want to add it to the configuration file (#configuration) in order not to type it every time you run youtube-dl....
Can you please put the -b option back?
Most people asking this question are not aware that youtube-dl now defaults to downloading the highest available quality as reported by YouTube, which will be 1080p or 720p in some cases, so you no longer need the -b option. For some specific videos, maybe YouTube does not report them to be available in a specific high quality format you're interested in. In that case, simply request it with the
-f
option and youtube-dl will try to download it.
Youtube (probably as it is supposed to be a video playing site) also apparently compresses the audio (see here) down to 120~KBs, which is quite low but for the most part seems OK.
To compare download videos, you can query the downloaded video/audio tracks (you may need to use avprobe
instead of ffprobe
) to see which is best:
$ ffprobe -hide_banner output-output-loads-of-options.webm
Input #0, matroska,webm, from 'output-output-loads-of-options.webm':
Metadata:
encoder : google
Duration: 00:04:02.63, start: 0.000000, bitrate: 118 kb/s
Stream #0:0(eng): Audio: vorbis, 44100 Hz, stereo, fltp (default)
$ ffprobe -hide_banner output_with_just_-x.ogg
Input #0, ogg, from 'output_with_just_-x':
Duration: 00:04:02.65, start: 0.000000, bitrate: 117 kb/s
Stream #0:0(eng): Audio: vorbis, 44100 Hz, stereo, fltp, 128 kb/s
Metadata:
LANGUAGE : eng
ENCODER : Lavf56.4.101