You are not logged in.

#1 2021-06-12 13:40:56

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 2,352
Website

youtube-dl for audio downloads - best command line options

johnraff wrote:

Youtube-dl a whole playlist, numbered tracks, in its own subfolder.

Throwing this in here because it's not worth a whole thread, and there doesn't seem to be a dedicated "media tweaks" topic.

I put this in ~/.bash_aliases but use it how you wish. (Of course replace $HOME/Downloads/youtube/ with your youtube downloads directory):

downloadpl() {
    youtube-dl "$@" -i -x -o "$HOME/Downloads/youtube/%(artist)s-%(playlist)s/%(playlist_index)s-%(title)s.%(ext)s" "$(xsel)"
}

If you've got xsel installed you just have to select the youtube url in the browser address bar before running downloadpl in a terminal.
(The "$@" is just so you can throw in any extra youtube-dl options - I seldom do though.)

^Neat, just a note:
Why '-x'? I think you can always just take AAC, with '-f 140' (and there should be no video downloading/extraction required). Also you probably want to add '-i' to keep downloading on error. I'd also swap /home/john with $HOME, unless there is specific reason to be exact?

Last edited by johnraff (2021-06-14 02:23:12)

Offline

#2 2021-06-13 02:29:04

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 8,156
Website

Re: youtube-dl for audio downloads - best command line options

brontosaurusrex wrote:

Why '-x'?

I left that in without thinking about it too much, because I very seldom want the attached video when I download music. It's usually worthless anyway, and occupies unnecessary disk space. People who want the video (and me too on occasions) can just leave it out.

man youtube-dl wrote:

Post-processing Options:
       -x, --extract-audio
              Convert  video  files to audio-only files (requires ffmpeg or av‐
              conv and ffprobe or avprobe)

       --audio-format FORMAT
              Specify  audio  format:  "best",  "aac",  "flac",  "mp3",  "m4a",
              "opus",  "vorbis", or "wav"; "best" by default; No effect without
              -x

("best" audio format is fine with me.)

I think you can always just take AAC, with '-f 140' (and there should be no video downloading/extraction required).

Sorry, I don't understand this. What command line are you suggesting?

Generally I prefer to leave it up to youtube-dl to choose the format with the best quality sound, even if it means unnecessary download bandwidth. People paying by the byte will have different priorities I guess.

Also you probably want to add '-i' to keep downloading on error.

Probably a good idea, thanks. Will add. (Up till now, I've tended to wait till an error showed, then re-run with -i if I felt like it.)

I'd also swap /home/john with $HOME, unless there is specific reason to be exact?

No reason, except some utilities prefer the hard-coded path and I got into the habit when making quick personal-use hacks, to save debugging later. For sharing, agreed $HOME would have been better. I'll change that too. smile


...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )

Introduction to the Bunsenlabs Lithium Desktop

Offline

#3 2021-06-13 04:00:58

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 2,352
Website

Re: youtube-dl for audio downloads - best command line options

johnraff wrote:

What command line are you suggesting?

From my notes, if audio only download is the idea

youtube-dl -i -f 140 playlist

And one can get a list of available streams with

youtube-dl -F url

Last edited by brontosaurusrex (2021-06-13 05:07:58)

Offline

#4 2021-06-13 05:16:47

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 8,156
Website

Re: youtube-dl for audio downloads - best command line options

brontosaurusrex wrote:
johnraff wrote:

What command line are you suggesting?

From my notes, if audio only download is the idea

youtube-dl -i -f 140 playlist

And one can get a list of available streams with

youtube-dl -F url

I've looked in the manual and haven't found a reference to '140'. Are you saying that 'youtube-dl -F url' should be run first in order to know what's available? If so, I think for a generic quick command -x might be better:

man youtube-dl wrote:

FORMAT SELECTION
       By default youtube-dl tries to download the best available quality, i.e.
       if you want the best quality you don't need to pass any special options,
       youtube-dl will guess it for you by default.

Last edited by johnraff (2021-06-13 05:17:26)


...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )

Introduction to the Bunsenlabs Lithium Desktop

Offline

#5 2021-06-13 05:22:34

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 2,352
Website

Re: youtube-dl for audio downloads - best command line options

I've looked in the manual and haven't found a reference to '140'. Are you saying that 'youtube-dl -F url' should be run first in order to know what's available?

No, only if you want something else than AAC, 140 (format code) is always m4a/AAC as it seems. Rick Astley example

youtube-dl -F "https://www.youtube.com/watch?v=dQw4w9WgXcQ" | grep 140

should return

140 m4a audio only tiny  129k , m4a_dash container, mp4a.40.2@129k (44100Hz), 3.27MiB

Again, this is assuming that video is not of interest at all.

Last edited by brontosaurusrex (2021-06-13 05:37:35)

Offline

#6 2021-06-13 07:37:35

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 8,156
Website

Re: youtube-dl for audio downloads - best command line options

brontosaurusrex wrote:

140 (format code) is always m4a/AAC as it seems

Where do you look this up?

And, what if m4a/AAC is not available for a certain video, or is not the best quality available?

Why not just pass '-x'?
Are you trying to avoid downloading video data which will be thrown away?

Most often, what I get with '-x' is .opus, sometimes .mp3 and some .m4a files: mpv can play them all, so I'm happy to get whichever download-yt thinks has the best available quality, without doing any unnecessary conversions.

Last edited by johnraff (2021-06-13 07:53:52)


...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )

Introduction to the Bunsenlabs Lithium Desktop

Offline

#7 2021-06-13 08:37:52

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 2,352
Website

Re: youtube-dl for audio downloads - best command line options

Where do you look this up?

-F for a stream url should list them all, 140 is always m4a

And, what if m4a/AAC is not available for a certain video, or is not the best quality available?

Don't have an answer for that (It may be missing, but more meta request like 'bestaudio' may be also missing), as of quality I doubt that youtube, much less youtube-dl actually knows what's the best quality, they probably mean bitrate. < educated guess. edit: Best quality is probably opus, but it is contained in webm, so some ffmpeg post will be made to 'opus' it (again unless you target it directly '-f 251').

Why not just pass '-x'?
Are you trying to avoid downloading video data which will be thrown away?

That and any ffmpeg postprocess is not required, file is fine as it is (would need benchmarking to prove). edit: Well -x takes 46s for that Rick Astley example and -f 140 needs 0.94s.

p.s. There is also '-f bestaudio' which takes webm/opus as expected and skips video/ffmpeg post as well (keeping opus in webm container which mpv is happy to play as is).

The conclusion for audio only scheme would be:
- Use '-f 140' to always get great quality AAC (when available) in its 'natural' container <  my pick
- Use '-f bestaudio' to always get great quality audio (when available) in possibly various formats
*Both are skipping any video downloads and any ffmpeg post.

Last edited by brontosaurusrex (2021-06-13 09:11:25)

Offline

#8 2021-06-14 01:21:11

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 8,156
Website

Re: youtube-dl for audio downloads - best command line options

^This is very interesting and I'd like to do some testbenching and continue the discussion, but it's threatening to spam out the thread, so let's fork it off.

EDIT: Done, and added a link from the original post.

Last edited by johnraff (2021-06-14 01:36:51)


...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )

Introduction to the Bunsenlabs Lithium Desktop

Offline

#9 2021-06-14 02:58:07

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 8,156
Website

Re: youtube-dl for audio downloads - best command line options

First off:

brontosaurusrex wrote:

-x takes 46s for that Rick Astley example and -f 140 needs 0.94s.

My results differ:

time youtube-dl -x "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
[youtube] dQw4w9WgXcQ: Downloading webpage
[download] Destination: Rick Astley - Never Gonna Give You Up (Video)-dQw4w9WgXcQ.webm
[download] 100% of 3.28MiB in 00:00
[ffmpeg] Destination: Rick Astley - Never Gonna Give You Up (Video)-dQw4w9WgXcQ.opus
Deleting original file Rick Astley - Never Gonna Give You Up (Video)-dQw4w9WgXcQ.webm (pass -k to keep)

real	0m2.586s
user	0m1.423s
sys	0m0.088s
#####
john@lithium:~/tmp$ time youtube-dl -f 140 "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
[youtube] dQw4w9WgXcQ: Downloading webpage
[download] Destination: Rick Astley - Never Gonna Give You Up (Video)-dQw4w9WgXcQ.m4a
[download] 100% of 3.27MiB in 00:00
[ffmpeg] Correcting container in "Rick Astley - Never Gonna Give You Up (Video)-dQw4w9WgXcQ.m4a"

real	0m2.441s
user	0m1.316s
sys	0m0.125s

Run time is almost the same: clearly neither -x nor -f 140 are downloading the video data. The .opus file is very slightly smaller than the .m4a but that's not important I guess. ffmpeg is invoked to extract the audio from the webm file, but I'm assuming that it's simply unpacking it from the webm container, not doing any real processing. Very open to being contradicted on that though.

Last edited by johnraff (2021-06-14 03:21:22)


...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )

Introduction to the Bunsenlabs Lithium Desktop

Offline

#10 2021-06-14 03:06:59

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 8,156
Website

Re: youtube-dl for audio downloads - best command line options

But my other point still remains, I think: to know that -f 140 is available for some particular download it's necessary to run 'youtube-dl -F' first. And sub-point: I haven't found anywhere where the format numbers are documented.

We seem to be talking past each other:

johnraff wrote:

Are you saying that 'youtube-dl -F url' should be run first in order to know what's available?

brontosaurusrex wrote:

Are you saying that 'youtube-dl -F url' should be run first in order to know what's available?

No, only if you want something else than AAC, 140 (format code) is always m4a/AAC as it seems.

johnraff wrote:
brontosaurusrex wrote:

140 (format code) is always m4a/AAC as it seems

Where do you look this up?

brontosaurusrex wrote:

Where do you look this up?

-F for a stream url should list them all, 140 is always m4a

So to be sure, you have to run -F first, which takes almost as long as the actual download:

john@lithium:~/tmp$ time youtube-dl -F "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
[youtube] dQw4w9WgXcQ: Downloading webpage
[info] Available formats for dQw4w9WgXcQ:
format code  extension  resolution note
249          webm       audio only tiny   49k , opus @ 50k (48000Hz), 1.18MiB
250          webm       audio only tiny   65k , opus @ 70k (48000Hz), 1.55MiB
140          m4a        audio only tiny  130k , m4a_dash container, mp4a.40.2@128k (44100Hz), 3.27MiB
251          webm       audio only tiny  136k , opus @160k (48000Hz), 3.28MiB
394          mp4        256x144    144p   76k , av01.0.00M.08, 25fps, video only, 1.74MiB
278          webm       256x144    144p   97k , webm container, vp9, 25fps, video only, 2.25MiB
160          mp4        256x144    144p  112k , avc1.4d400c, 25fps, video only, 2.78MiB
395          mp4        426x240    240p  159k , av01.0.00M.08, 25fps, video only, 3.40MiB
242          webm       426x240    240p  217k , vp9, 25fps, video only, 4.04MiB
133          mp4        426x240    240p  251k , avc1.4d4015, 25fps, video only, 6.17MiB
396          mp4        640x360    360p  330k , av01.0.01M.08, 25fps, video only, 6.63MiB
243          webm       640x360    360p  396k , vp9, 25fps, video only, 6.96MiB
397          mp4        854x480    480p  571k , av01.0.04M.08, 25fps, video only, 11.32MiB
244          webm       854x480    480p  586k , vp9, 25fps, video only, 10.03MiB
134          mp4        640x360    360p  633k , avc1.4d401e, 25fps, video only, 12.98MiB
247          webm       1280x720   720p 1035k , vp9, 25fps, video only, 17.67MiB
398          mp4        1280x720   720p 1081k , av01.0.05M.08, 25fps, video only, 21.71MiB
135          mp4        854x480    480p 1167k , avc1.4d401e, 25fps, video only, 26.06MiB
399          mp4        1920x1080  1080p 2068k , av01.0.08M.08, 25fps, video only, 39.81MiB
136          mp4        1280x720   720p 2317k , avc1.4d401f, 25fps, video only, 53.38MiB
137          mp4        1920x1080  1080p 4466k , avc1.640028, 25fps, video only, 107.63MiB
18           mp4        640x360    360p  601k , avc1.42001E, 25fps, mp4a.40.2@ 96k (44100Hz), 15.19MiB (best)

real	0m1.763s
user	0m1.102s
sys	0m0.045s

...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )

Introduction to the Bunsenlabs Lithium Desktop

Offline

#11 2021-06-14 03:19:16

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 8,156
Website

Re: youtube-dl for audio downloads - best command line options

Oh yes:

john@lithium:~/tmp$ time youtube-dl -f bestaudio "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
[youtube] dQw4w9WgXcQ: Downloading webpage
[download] Destination: Rick Astley - Never Gonna Give You Up (Video)-dQw4w9WgXcQ.webm
[download] 100% of 3.28MiB in 00:00

real	0m2.316s
user	0m1.153s
sys	0m0.051s

and

# webm, via -f bestaudio
john@lithium:~/tmp$ ffprobe Rick\ Astley\ -\ Never\ Gonna\ Give\ You\ Up\ \(Video\)-dQw4w9WgXcQ.webm 
# snip #
Input #0, matroska,webm, from 'Rick Astley - Never Gonna Give You Up (Video)-dQw4w9WgXcQ.webm':
  Metadata:
    encoder         : google/video-file
  Duration: 00:03:32.06, start: -0.007000, bitrate: 129 kb/s
    Stream #0:0(eng): Audio: opus, 48000 Hz, stereo, fltp (default)

# opus, via -x
john@lithium:~/tmp$ ffprobe Rick\ Astley\ -\ Never\ Gonna\ Give\ You\ Up\ \(Video\)-dQw4w9WgXcQ.opus 
# snip #
Input #0, ogg, from 'Rick Astley - Never Gonna Give You Up (Video)-dQw4w9WgXcQ.opus':
  Duration: 00:03:32.07, start: 0.007500, bitrate: 127 kb/s
    Stream #0:0(eng): Audio: opus, 48000 Hz, stereo, fltp
    Metadata:
      LANGUAGE        : eng
      ENCODER         : Lavf58.20.100

# m4a, via -f 140
john@lithium:~/tmp$ ffprobe Rick\ Astley\ -\ Never\ Gonna\ Give\ You\ Up\ \(Video\)-dQw4w9WgXcQ.m4a 
# snip #
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'Rick Astley - Never Gonna Give You Up (Video)-dQw4w9WgXcQ.m4a':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2mp41
    encoder         : Lavf58.20.100
  Duration: 00:03:32.09, start: 0.000000, bitrate: 129 kb/s
    Stream #0:0(eng): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default)
    Metadata:
      handler_name    : ISO Media file produced by Google Inc.

...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )

Introduction to the Bunsenlabs Lithium Desktop

Offline

#12 2021-06-14 04:32:02

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 2,352
Website

Re: youtube-dl for audio downloads - best command line options

Well, now I'am confused by my confusion smile.

I wouldn't precheck anything, but to test the magic number 140 one could illustrate the situation with

youtube-dl -F "$playlist" | grep '^140\|Downloading video'

And I'am not aware of any public documentation of this numbers.

Since this kind of talking to cloud is blackbox approach, I think you need to assume stuff. I'd use -i to simply skip stuff that didn't work (One can always run the same command and hope there will be no skips the next time).  You can assume that m4a stream will be there (if there is audio) since certain devices will prefer that (but yeah youtube can change all that tomorrow).

If you think youtube-dl knows better, use one of the more meta switches. -bestaudio

If you need to be extra sure that the file downloaded is really m4a/AAC or opus, ffprobe or mediainfo can be of help offline in 2nd step. This highly simplistic approach seems to be working as well, just checking if it hasAudio

#!/bin/bash

# hasAudio?

# help
if [ $# -eq 0 ]; then echo "Returns true if file has audio (mediainfo)"; exit 1; fi
# checks
command -v mediainfo >/dev/null 2>&1 || { >&2 echo "I need mediainfo installed." ; exit 1; }
# main
mediainfo "$1" 2>/dev/null | grep -v "Audio Video Interleave" | grep -qi "audio"

Usage:

hasAudio "$path" && echo "yes"

I don't know how the youtube-dl -x switch is behaving exactly, but obviously it sometimes takes a lot of time (I'd avoid that).

What I learned, unrelated: Certain streams seem to have low-quality AV1 video encodes as well (no grain synth visible) - purpose unknown, if I'd have to guess they are probably targeting 'super-low-bitrate situations' (stream 399 in Rick example).

Last edited by brontosaurusrex (2021-06-14 07:25:53)

Offline

#13 2021-06-14 07:51:43

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 8,156
Website

Re: youtube-dl for audio downloads - best command line options

brontosaurusrex wrote:

I don't know how the youtube-dl -x switch is behaving exactly, but obviously it sometimes takes a lot of time

To be honest, that's not so obvious to me. -x has never taken a particulary long time for me. Your difference of 46s vs 0.94s is quite inexplicable. Not some unrelated network instability?

Looking at the output on these downloads, it seems to me the only difference between -x and -f bestaudio is that the opus is extracted from the downloaded webm. The download itself looks identical.

brontosaurusrex wrote:

...as of quality I doubt that youtube, much less youtube-dl actually knows what's the best quality...

Really this is the difference between us I guess. I'm prepared to rely on the youtube-dl devs to know their stuff and just send -x meaning "do your best". For a quick-and-dirty rip of a free bit of audio from a public wiki that's all the time and effort I feel like putting in. roll

Those who want to squeeze out the last drop of quality will want to follow your advice. smile


...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )

Introduction to the Bunsenlabs Lithium Desktop

Offline

#14 2021-06-14 10:33:27

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 2,352
Website

Re: youtube-dl for audio downloads - best command line options

johnraff wrote:

Your difference of 46s vs 0.94s is quite inexplicable. Not some unrelated network instability?

Could be, much longer test with 148 clips

youtube-dl -i -x       903,43s user 88,14s system 93% cpu 17:46,16 total
youtube-dl -i -f 140   751,78s user 90,24s system 37% cpu 37:04,67 total

Looks significant, but for the  -x side.

Last edited by brontosaurusrex (2021-06-14 10:36:35)

Offline

#15 2021-06-14 13:00:31

twoion
ほやほや
Registered: 2015-08-10
Posts: 3,245

Re: youtube-dl for audio downloads - best command line options

AFAICT -x downloads the 'best' audio AND video stream then extracts the audio. Direct audio-only format selection using -f can make do with only downloading audio so depending on John's maximum download speed it can take the same time but doesn't have to.

For what it's worth, I recently pruned my audio library by removing video streams from YT "audio" downloads. I had quite a few GB saved using this way.

 cat /home/joj/.local/bin/stripvideo
#!/usr/bin/env bash

set -e

file_format () {
  local path=$1
  mediainfo \
    '--Inform=General;%Format%' \
    -- "$path" \
    | tr '[:upper:]' '[:lower:]'
  return $?
}

video_count () {
  local path=$1
  mediainfo \
    '--Inform=General;%VideoCount%' \
    -- "$path"
  return $?
}

audio_count () {
  local path=$1
  mediainfo \
    '--Inform=General;%AudioCount%' \
    -- "$path"
  return $?
}

audio_format () {
  local path=$1
  mediainfo \
    '--Inform=Audio;%Format%' \
    -- "$path" \
    | tr '[:upper:]' '[:lower:]'
  return $?
}

output_file_path () {
  local path=$1
  local ext=$2
  if [[ $path =~ \.???$ ]]; then
    printf "%s\n" "${path%.???}.$ext"
  else
    printf "%s\n" "$path.$ext"
  fi
}

for file in "$@"; do
  file_format=$(file_format "$file")
  case "$file_format" in
    webm|mpeg-4|matroska)
      true ;;
    *)
      echo "Skipping unknown format: $file_format in $file"
      continue
      ;;
  esac
  ac=$(audio_count "$file")
  vc=$(video_count "$file")
  if (( $ac != 1 )); then
    echo "Skipping file with no audio or multiple audio streams: $file"
    continue
  fi
  if (( $vc == 0 )); then
    echo "Skipping file with no video to strip: $file"
    continue
  fi
  fmt=$(audio_format "$file")
  case "$fmt" in
    opus)
      of=$(output_file_path "$file" opus)
      ffmpeg -i "$file" -vn -acodec copy "$of"
      rm -- "$file"
      ;;
    aac*)
      of=$(output_file_path "$file" aac)
      ffmpeg -i "$file" -vn -acodec copy "$of"
      rm -- "$file"
      ;;
    vorbis)
      of=$(output_file_path "$file" ogg)
      ffmpeg -i "$file" -vn -acodec copy "$of"
      rm -- "$file"
      ;;
    *)
      echo "Skipping file with unknown audio format: $fmt in $file"
      continue
      ;;
  esac
done

Note that this script should be improved regarding aac files. aac is not supposed to survive in a bare format but should be embedded into a mp4 container, yielding m4a files. I'm currently using this command to embed aac files in mp4 containers:

for x in *.aac; do o=${x%.aac}.m4a; ffmpeg -i "$x" -map 0:a -c:a copy "$o" &>/dev/null && echo "$x: OK" || echo "$x: ERROR"; done

aac files can be tagged with SOME software and can be played back by SOME software, but for example I had problems getting mpd to seek in bare aac streams whereas mpv includes enough hacks to be able to do just that.

For mangling mkvs however I strongly prefer mkvextract/mkvmerge because they have less arcane syntax than ffmpeg. Also, mkvinfo will dump information along with comments how to target specific tracks, chapters etc using mkvmerge/mkvextract.


Nassdachs

Offline

#16 2021-06-15 02:37:21

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 8,156
Website

Re: youtube-dl for audio downloads - best command line options

@twoion, thanks for the file conversion help.

As for this:

twoion wrote:

AFAICT -x downloads the 'best' audio AND video stream then extracts the audio.

I was starting to wonder if I lived in a different universe from you two guys, because that's quite contrary to my experience. As you can see in the terminal o/p I posted above, for me "-x" takes almost exactly as long to run as  "-f bestaudio" or "-f 140". The manual didn't shed any light, but inspiration came with the youtube-dl README on GitHub: https://github.com/ytdl-org/youtube-dl#format-selection

youtube-dl wrote:

Since the end of April 2015 and version 2015.04.26, youtube-dl uses -f bestvideo+bestaudio/best as the 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 the 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.

cZ2cA7K.gif
So, the key is that YouTube do provide the video and audio in separate files, so 'youtube-dl -x' is able to download the audio only. In the general case, though, some sites only provide a single file so you'd be correct that '-x' would download the video, then extract the audio locally.

Running youtube-dl -x with and without the -k option makes it clear. In the second case, only 3.28MiB of data is downloaded:

john@lithium:~/tmp$ youtube-dl -x -k "https://www.youtube.com/watch?v=dQw4w9WgXcQ"[youtube] dQw4w9WgXcQ: Downloading webpage
WARNING: Requested formats are incompatible for merge and will be merged into mkv.
[download] Destination: Rick Astley - Never Gonna Give You Up (Video)-dQw4w9WgXcQ.f137.mp4
[download] 100% of 107.63MiB in 00:05
[download] Destination: Rick Astley - Never Gonna Give You Up (Video)-dQw4w9WgXcQ.f251.webm
[download] 100% of 3.28MiB in 00:00
[ffmpeg] Merging formats into "Rick Astley - Never Gonna Give You Up (Video)-dQw4w9WgXcQ.mkv"
[ffmpeg] Destination: Rick Astley - Never Gonna Give You Up (Video)-dQw4w9WgXcQ.opus

john@lithium:~/tmp$ youtube-dl -x "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
[youtube] dQw4w9WgXcQ: Downloading webpage
[download] Destination: Rick Astley - Never Gonna Give You Up (Video)-dQw4w9WgXcQ.webm
[download] 100% of 3.28MiB in 00:00
[ffmpeg] Destination: Rick Astley - Never Gonna Give You Up (Video)-dQw4w9WgXcQ.opus
Deleting original file Rick Astley - Never Gonna Give You Up (Video)-dQw4w9WgXcQ.webm (pass -k to keep)

Panic over. smile


...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )

Introduction to the Bunsenlabs Lithium Desktop

Offline

#17 2021-06-15 02:49:33

hhh
Meep!
Registered: 2015-09-17
Posts: 12,021
Website

Re: youtube-dl for audio downloads - best command line options

Dammit, Rickrolled again!

Offline

#18 2021-06-15 04:20:34

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 8,156
Website

Re: youtube-dl for audio downloads - best command line options

^It was the Lizard King who slipped that in.  yikes

Shows how out of touch I am - had to look it up.


...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )

Introduction to the Bunsenlabs Lithium Desktop

Offline

#19 2021-06-15 05:21:33

ohnonot
...again
Registered: 2015-09-29
Posts: 5,520

Re: youtube-dl for audio downloads - best command line options

On youtube itself, format 140 might always be available, but this is not guaranteed on all sites.
As far as I can see, even those numeric format codes are specific to youtube.

But youtube-dl makes intelligent choices for many more sites.
Obviously it's faster if you tell it to NOT make those choices and just blindly go for 140, but it's not guaranteed to work.

And even for youtube, 140 probably is never the best audio quality available.

And that's all there is to it.

Frankly, I think they should have renamed that software long ago, but I guess it's too late now.
Just need to keep reminding people that youtube-dl is so much more versatile than just youtube.com.

PS: brontosaurusrex' argument makes sense on a metered or generally slow internet connection - but, again, only for youtube itself.
Also, the term "convert" (used by johnraff) is misleading - the audio track itself is not converted, just isolated from the video it's been muxed together with. There's no quality loss.
Generally youtube-dl devs are very concerned about obtaining the best possible quality results.

Last edited by ohnonot (2021-06-15 05:32:34)


Give to COVAX! Here or here. (explanation)

Offline

#20 2021-06-15 05:33:33

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 2,352
Website

Re: youtube-dl for audio downloads - best command line options

@johnraff, You were right about -x and I was wrong, standing ashamed and corrected (face pointing down). smile

Last edited by brontosaurusrex (2021-06-15 12:05:47)

Offline

#21 2021-06-15 05:38:05

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 8,156
Website

Re: youtube-dl for audio downloads - best command line options

ohnonot wrote:

Also, the term "convert" (used by johnraff) is misleading - the audio track itself is not converted, just isolated from the video it's been muxed together with. There's no quality loss.

Quite agree, but don't blame me - it was a quote from man youtube-dl:

johnraff wrote:
man youtube-dl wrote:

Post-processing Options:
       -x, --extract-audio
              Convert  video  files to audio-only files (requires ffmpeg or av‐
              conv and ffprobe or avprobe)

That's the only instance of "convert" on this page.


...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )

Introduction to the Bunsenlabs Lithium Desktop

Offline

Board footer

Powered by FluxBB