22. March 2014 2 min read

Download videos from YouTube and convert to mp3 in Linux terminal

Did you ever wanted to download video from YouTube to listen of it on your mobile while you do not have internet access? And of course you are in Linux as it is simplest to work with Andorid phones. To download video from Youtube the good program is youtube-dl and to convert it you will use avconv (former ffmpeg). It is very simple to download YouTube video from terminal as well as conversion. Just few commands.

# download video from YouTube
youtube-dl http://url_to_video

# convert mp4 file to mp3 
avconv -i video_filename.mp4 convert_to_this_file.mp3

# above steps with newer youtube-dl can be done using below command
youtube-dl --extract-audio --audio-format mp3 --prefer-avconv --embed-thumbnail https://url_to_video

I know some people are not familiar with bash, so here is a script that converts all mp4 files to mp3 files. It is not needed anymore since youtube-dl does conversion with last command. I am sure you can improve it easily but still:

#!/bin/bash
# loop through each file in directory
for file in /path/to/directory/*.mp4
do
     # convert each file into mp3
     avconv -i "$file" "$file.mp3"
     # remove the old mp4 file
     rm "$file"
done

Newest from this category: