8

I would like to connect to YouTube and read a video only for audio with commands. Is that possible?

Braiam
  • 35,380
  • 25
  • 108
  • 167
aleroy
  • 133
  • 6
  • VLC can play Youtube videos, you should fiddle around with the command line options to disable GUI, video and tell it the correct audio device to use –  Jul 19 '14 at 20:54

2 Answers2

13

To download the audio as mp3, naming the file including the authors name and the video title, use:

youtube-dl -x --audio-format mp3 '<URL>'


For example, to download that interesting talk of Linus on git, and include the uploader's name in the file name (*),

youtube-dl -x --audio-format mp3 --output '%(uploader)s-%(title)s.%(ext)s' 'https://www.youtube.com/watch?v=4XpnKHJAok8'

will save the audio as file
Google-Tech Talk - Linus Torvalds on git.mp3


To date, this needs to download the full video data, and split the audio part off, discarding the images.
But it is expected that it will be possible to download just the audio track soon, with one of the next updates of youtube-dl, combined with changes at the YouTube server side.

youtube-dl then uses ffmpeg to convert the downloaded .m4a file to the .mp3 format.

To make the command line shorter, options can be saved in the config file:
~/.config/youtube-dl.conf

* -x is the short form of --extract-audio

Volker Siegel
  • 16,983
  • 5
  • 52
  • 79
  • It do not work: [download] 100% of _size_ .MiB in _time_ ERROR: ffprobe or avprobe not found. Please install one. I think it is because of ffmpeg. – aleroy Jul 19 '14 at 12:35
  • @aleroy Yes, right. (Try apt-get install ffmpeg or equivalent) – Volker Siegel Jul 19 '14 at 13:09
  • All this suppose to download the video but what if we do not download it, but we just read it? How can I do that? – aleroy Jul 19 '14 at 13:37
  • What do you mean by "just read"? The audio is mixed with the video in one file, so we need to get that file first. – Volker Siegel Jul 19 '14 at 13:39
  • Why are you passing in `--restrict-filenames`? That's not necessary at all. Also note that any recent version of youtube-dl [does only download the audio for YouTube videos](https://github.com/rg3/youtube-dl/blob/de3ef3ed/youtube_dl/__init__.py#L627). – phihag Jul 19 '14 at 15:29
  • @phihag `--restrict-filenames` removes various "special" characters from the output file name - if there are any, in title or author name. – Volker Siegel Jul 19 '14 at 15:33
  • @phihag Interesting - so there is support for audio-only from YouTube side now? So I need to revise the commands... – Volker Siegel Jul 19 '14 at 15:36
  • @Volker Siegel I'm well aware of that (given that I wrote the option), but what is the problem with these special characters? The OP is on Mac OSX, and unless he's using a really weird combination (like a thumb drive with a FAT32 filesystem from another locale) all characters should be supported. – phihag Jul 19 '14 at 15:39
  • @VolkerSiegel Yes, see [user287782's answer](http://unix.stackexchange.com/a/145438/5303). Unfortunately, the exact file format of YouTube's audio-only files is not supported by all music players. – phihag Jul 19 '14 at 15:41
  • Sure, I tried to express that I was not aware of it, and pleasantly surprised! On the filename characters: I do not have an example, but remember there were name examples that were technically valid file names (on ext4), but were hard to use in the shell. The option fixed that. Maybe it was just the "&" and the spaces (possibly leading spaces). – Volker Siegel Jul 19 '14 at 15:46
  • @VolkerSiegel The ampersand is only a problem if you type out the whole name, and I don't know who would do that. I don't think a lot of people use a shell without tab completion. I've proposed an edit to simplify the answer. youtube-dl will filter out completely unreasonable characters in the file name automatically. – phihag Jul 19 '14 at 16:01
  • @VolkerSiegel I wanted something to read the music without downloading it, extracting the music from the video in cahce or something... But that's great! Thank you :) – aleroy Jul 27 '14 at 14:15
6

You can use youtube-dl -f bestaudio <URL> to download the audio of the video and mplayer to play it.

Khaelex
  • 161
  • 3