Youtube
Let’s say you want to watch a lecture on youtube and you need to also visit some of the links mentioned in the description or read some of the references, but you can’t do them all online. Of course you’d want to download the video and its description for offline use. So, Here is what I’ve done for myself. The following code downloads the video into your Videos folder under the home directory and then converts it to an mp3 file so that you can listen to it on the go. It also creates an html file containing the descriptions of the video. The code consists of two parts:
- The converter to mp3:
- The downloader
If you don’t want to convert it to mp3 then ignore that part and delete the corresponding line from the second code.
- The converter to mp3: I ~~don't remember where I got this from but it's not my code~~ stole the code from one of the answers [here](https://askubuntu.com/questions/84584/converting-mp4-to-mp3) (How can I refer to a specific answer in stack exchange?). It requires `mplayer` and `lame` to be installed. It basically converts anything that your mplayer plays to an mp3 file. I saved this file under `convert2mp3`.
#! /bin/bash
# Converts to mp3 anything mplayer can play
# Needs mplayer and lame installed
[ $1 ] || { echo "Usage: $0 file1.wma file2.wma"; exit 1; }
for i in "$@"
do
[ -f "$i" ] || { echo "File $i not found!"; exit 1; }
done
[ -f audiodump.wav ] && {
echo "file audiodump.wav already exists"
exit 1
}
for i in "$@"
do
ext=`echo $i | sed 's/[^.]*\.\([a-zA-Z0-9]\+\)/\1/g'`
j=`basename "$i" ".$ext"`
j="$j.mp3"
echo
echo -n "Extracting audiodump.wav from $i... "
mplayer -vo null -vc null -af resample=44100 -ao pcm:waveheader:fast \
"$i" >/dev/null 2>/dev/null || {
echo "Problem extracting file $i"
exit 1
}
echo "done!"
echo -n "Encoding to mp3... "
lame -m s audiodump.wav -o "$j" >/dev/null 2>/dev/null
echo "done!"
echo "File written: $j"
done
# delete temporary dump file
rm -f audiodump.wav
- The downloader: I save this file under
ytd.
#!/bin/bash
# download a video from youtube and convert it to mp3
# it requires the script convert2mp3
if [[ $(echo $*) ]]; then
searchterm="$*"
else
read -p "Enter the video ID: " searchterm
fi
searchterm=$(echo $searchterm | sed -e 's/\ /+/g')
video=$(youtube-dl -g https://youtube.com/v/$searchterm)
Title=$(curl -s https://www.youtube.com/watch?v=$searchterm | grep -o '<title>.*</title>' | sed 's/\(<title>\| - YouTube<\/title>\)//g')
Name=$(echo ${Title// /_})
[[ -f ~/Videos/$Name.mp4 ]] || wget -O - $video | tee ~/Videos/$Name.mp4 2>&1 > /dev/null
[[ -f ~/Videos/$Name.html ]] || echo '<html><body>' >> ~/Videos/$Name.html && curl -s https://www.youtube.com/watch?v=$searchterm | grep -o '<p id="eow-description" >.*</p>' | sed 's/\(<p id="eow-description" >\|<\/p>\)//g' >> ~/Videos/$Name.html && echo '</body></html>' >> ~/Videos/$Name.html
[[ -f ~/Videos/$Name.mp3 ]] || convert2mp3 ~/Videos/$Name.mp4
The Title reads the title of the video from the youtube page and Name changes the spaces in the title to underscores so that it can be easily passed to other commands. It would be neat if I could fix it so that it didn’t need to do so. Then if the file already doesn’t exist, it downloads it to the Videos folder under the home directory, then it gets the info in the description part of the webpage and converts it into a minimal html file with the same name and finally it converts the video to a mp3 file.
To use the code for example to download https://www.youtube.com/watch?v=ZK3O402wf1c&list=PLE7DDD91010BC51F8 call it in terminal as ytd ZK3O402wf1c where ZK3O402wf1c is the youtube video ID. In the url of the video the part between v= and first & is the video ID.
PS: You might have noticed that in the second code I have a few html tags in the commands since I’m reading a webpage and extracting things from it. Wordpress is not very friendly with those, and it makes sense. So I was wondering what should I do with them to make them show up correctly in the code and not get interpreted which I found this nice article: Writing Code In Your Posts. But that was a lot of work. So, I ended up adding a <pre> </pre> part to the html code of the post and then in the visual editor pasted the code, and the visual editor did all those conversions for me. I think I could add the code in the visual editor and then go to html code to add the <pre> </pre> tags.
To do:
Add the link of the youtube video to the html file.
Leave a comment