Simple YouTube to MP3 bash & batch scripts using VLC

About encoding, codec settings, muxers and filter usage
Yambam
New Cone
New Cone
Posts: 3
Joined: 20 Aug 2016 15:46
VLC version: 2.2.4
Operating System: Windows 10, Raspbian

Simple YouTube to MP3 bash & batch scripts using VLC

Postby Yambam » 20 Aug 2016 21:00

Hello,

I made a simple script in bash and also ported it to batch that lets you input a YouTube video ID and a filename as arguments and automatically downloads and converts it to an MP3 file. :D

Bash:

Code: Select all

#!/bin/sh # Dependencies: vlc, lame. Use "sudo apt-get install vlc lame" to install them. :) # Usage note: sh youtube2mp3.sh YOUTUBE-VIDEO-ID DEST filename=$(basename "$2") extension="${filename##*.}" filename="${filename%.*}" vlc -I dummy "http://www.youtube.com/watch?v=$1" ":sout=#transcode{acodec=s16l,channels=2}:std{access=file,mux=wav,dst=$filename.wav }" vlc://quit if [ "$extension" = "mp3" ]; then echo $extension is mp3 lame -h "$filename.wav" "$(echo $2 | cut -f 1 -d '.').mp3" # Uncomment the following line to remove the .wav file after having converted it to mp3. #rm "$filename.wav" fi
Batch (only for Windows):

Code: Select all

:: Dependencies: - VLC Media Player (http://www.videolan.org/vlc/) :: - LAME encoder (http://www.rarewares.org/mp3-lame-bundle.php) :: NOTE: You should add a semicolon and then the path to your VLC Media Player installation :: to your PATH environment variable, e.g. ";C:\Program Files\VideoLAN\VLC". :: IMPORTANT: Don't add a space between the path and the semicolon! :: Usage note: youtube2mp3 <youtube-video-id> <filename> @echo off start /w "" vlc -I dummy http://www.youtube.com/watch?v=%1 ":sout=#transcode{acodec=s16l,channels=2}:std{access=file,mux=wav,dst=%~dpn2.wav }" vlc://quit if "%~x2"==".mp3" ( start /w "" vlc -I dummy http://www.youtube.com/watch?v=%1 ":sout=#transcode{acodec=s16l,channels=2}:std{access=file,mux=wav,dst=%~dpn2.wav }" vlc://quit lame -h "%~dpn2.wav" "%~dpn2.mp3" :: Uncomment the following line to remove the .wav file after having converted it to mp3. :: :: rm "%~dpn2.wav" )
Any suggestions are welcome! :)

Yambam
New Cone
New Cone
Posts: 3
Joined: 20 Aug 2016 15:46
VLC version: 2.2.4
Operating System: Windows 10, Raspbian

Re: Simple YouTube to MP3 bash & batch scripts using VLC

Postby Yambam » 15 Sep 2016 11:01

New version (bash):

Code: Select all

#!/bin/sh # Dependencies: vlc, lame, mpg123. Use "sudo apt-get install vlc lame mpg123" to install them. :) # Usage note: sh youtube2mp3.sh YOUTUBE-VIDEO-ID DEST cd ~/Music basename_ext="${2##*/}" extension="${basename_ext##*.}" basename="${basename_ext%.*}" filename="${2%.*}" echo "Downloading YouTube/$1 to $2...\n" if [ -f "$basename.wav" ]; then aplay "$basename.wav" elif [ -f "$filename.mp3" ]; then mpg123 "$filename.mp3" else vlc -I dummy "http://www.youtube.com/watch?v=$1" ":sout=#transcode{acodec=s16l,channels=2}:std{access=file,mux=wav,dst=\"$basename.wav\" }" vlc://quit if [ "$extension" = "mp3" ]; then #echo $extension is mp3 lame -h "$basename.wav" "$filename.mp3" # Uncomment the following line to remove the .wav file after having converted it to mp3. rm "$basename.wav" mpg123 "$filename.mp3" fi fi
Fixed:
  • Filenames containing apostrophes
New stuff:
  • Plays automatically using mpg123 if the extension is .mp3
  • When the destination file exists, it will be played instead
To do:
  • Download the .mp4 file directly instead of converting to .wav, does anyone know if converting .mp4 -> .mp3 is faster than .wav -> .mp3?
Extra:
Here's another little something. This script will search for mp3 files in ~/Music like "01-Yann Tiersen - Comptine d'un autre été.mp3" (notice '01-' at the beginning), make a playlist and play them in order using mplayer:

Code: Select all

#!/bin/bash ls -1 ~/Music/??-* > ~/Music/ls.m3u mplayer --ao=alsa --mixer-channel=PCM -playlist ~/Music/ls.m3u
The youtube-to-mp3 script by default saves music in ~/Music too, so you don't have to specify the directory in that script before calling this script. For extra ease you can add a path to your PATH environment variable in ~/.profile, simply replace $PATH with $PATH:~/bashscripts for example. And so you can call it without the sh command, mark the scripts as executable like so: chmod +x youtube-to-mp3 music-bg


Return to “VLC stream-output (sout)”

Who is online

Users browsing this forum: No registered users and 35 guests