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:

  1. The converter to mp3:
  2. 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.

  1. 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 (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 "$@"
    [ -f "$i" ] || { echo "File $i not found!"; exit 1; }

[ -f audiodump.wav ] && {
    echo "file audiodump.wav already exists"
    exit 1

for i in "$@"
    ext=`echo $i | sed 's/[^.]*\.\([a-zA-Z0-9]\+\)/\1/g'`
    j=`basename "$i" ".$ext"`
    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"
# delete temporary dump file
rm -f audiodump.wav

2. The downloader: I save this file under ytd.

# download a video from youtube and convert it to mp3
# it requires the script convert2mp3

if [[ $(echo $*) ]]; then
    read -p "Enter the video ID: " searchterm

searchterm=$(echo $searchterm | sed -e 's/\ /+/g')

video=$(youtube-dl -g$searchterm)
Title=$(curl -s$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$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 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 Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s