Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

A script to download and convert multiple YouTube videos UNIX
There are many solutions on the web on how to download YouTube videos using bash scripts, e.g. see this one as an example. However, using scripts described in the above link may be a little uncomfortable if you wish to get lots of YouTube clips. So I've modified those scripts a little bit so that the URLs are put into a single file with this structure:
url1
name1
url2
name2
...
urlN
nameN
where urlN and nameN are Nth clip's URL and desired local filename for the saved movie file.

YouTube's FLV files are also converted to AVI format via ffmpeg (see this hint and comments on how to get and install it). I prefer AVI to FLV since most players have problems with FLV movies when it comes to scrolling through them.

Here is the script:
#!/bin/bash

# Usage:
# script_name inp
# The script downloads the movies from YouTube as specified in the given file (inp).
# The list of movies has to specified in the inp file as follows:

# url1
# file1
# url2
# file2
# ...
# urlN
# fileN

# urli and filei are the url and the output filename of the i-th movie.
# I used original script written by Crouse
# (see http://www.bashscripts.org/viewtopic.php?t=210&postdays=0&postorder=asc&start=25).

exec 10<$1
let nl=0
while read mov<&10; do
  l[$nl]=$mov
  ((nl++))
done

exec 10>&-
((k = nl % 2))
if [ $k != 0 ]; then
  echo "Wrong number of lines in file "$1"!"
  exit
fi
((n = nl / 2 - 1))

BASEURL="http://youtube.com/get_video.php?"
WDIR=YouTube
mkdir -p $WDIR
cd $WDIR 

for i in $(seq 0 $n); do
  ((iurl = 2 * i))
  ((imov = iurl + 1))
  URL0=${l[$iurl]}
  MOVNAME=${l[$imov]}
  echo $MOVNAME
  wget ${URL0} -O urlsource.txt ; 
  grep "watch_fullscreen" urlsource.txt > url.info; 
  VIDURL=`sed "s;.*(video_id.+)&title.*;1;" url.info`
  FULLURL=${BASEURL}${VIDURL}
  echo ${FULLURL}
  rm *
  wget ${FULLURL}
  mv * "${MOVNAME}"'.flv'
  ffmpeg -i "${MOVNAME}"'.flv' -ar 48000 -ac 2 "${MOVNAME}"'.avi'
  mv *.avi ../
done

cd ..
rm -rf $WDIR
To use the script, make it executable, then call it like this:
script_name file_with_urls_and_filenames
[robg adds: I haven't tested this one.]
    •    
  • Currently 2.57 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (7 votes cast)
 
[30,447 views]  

A script to download and convert multiple YouTube videos | 10 comments | Create New Account
Click here to return to the 'A script to download and convert multiple YouTube videos' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A script to download and convert multiple YouTube videos
Authored by: babbage on Feb 29, '08 09:06:00AM

Overriding core commands, especially `rm`, is a tempting but terrible idea.

It's all well & good to set yourself up with a safety net so that typing `rm` doesn't accidentally cause irreversible harm on your own computer, but what happens if you upgrade, or are working on someone else's computer, and forget that your little utility isn't providing the safety net any more?

It's a bad habit to get in to in the first place.

If you want to do something like this, give the utility function (alias, script, whatever) a different name, like "rmtrash" or just "rmt". That way, if you get in the habit of typing "rmt *" on your computer, it'll just not work elsewhere, rather than do something unexpected and unwanted.

Any good Unix book will dissuade you from using this kind of override, and there are very good reasons to heed that advice.

---

--
DO NOT LEAVE IT IS NOT REAL



[ Reply to This | # ]
A script to download and convert multiple YouTube videos
Authored by: babbage on Feb 29, '08 09:08:10AM
And of course, only on hitting Submit to I realize that I've attached this comment to the wrong article. *dopeslap!* :-)

http://www.macosxhints.com/article.php?story=20080224175659423

I'd paste it to the right one, but it looks like others have already beat me to it.

---

--
DO NOT LEAVE IT IS NOT REAL

[ Reply to This | # ]

A script to download and convert multiple YouTube videos
Authored by: Catherine on Feb 01, '10 07:35:06PM
iTube Studio is another good one.

[ Reply to This | # ]
A script to download and convert multiple YouTube videos
Authored by: auco on Feb 29, '08 10:05:50AM

not sure if this functionality came with SafariStand, but I can simply click the middle mouse button over any flash video to save the .flv the download folder



[ Reply to This | # ]
A script to download and convert multiple YouTube videos
Authored by: chleuasme on Feb 29, '08 10:55:53PM
Some comments :

  • Wget isn't installed with OS X, but cURL does
  • seq isn't installed with OS X, but bash offers a built-in syntax for sequences (in recents versions), or you can here directly use the for C-syntax (see man bash ;-)
  • I can't download a video with the url you obtain using Wget or cURL, having to do more (see the script below)

    Based on your script, you can have a try on this one :

    #!/bin/bash
    
    if [ $# -ne 1 ]
    then
        echo "Usage: $0 file"
        echo "where the file is composed of a list of YouTube URL on each line"
        exit
    fi
    
    function _parse_url {
        echo $2 | sed "s:.*[?&]${1}=\([^&\';]*\).*:\1:"
    }
    
    BASEURL="http://youtube.com/get_video.php?"
    
    while read 
    do
        tmp=$( curl -s "$REPLY" | grep watch_fullscreen) || continue
        id=$( _parse_url video_id "$tmp" )
        t=$( _parse_url t "$tmp" )
        title=$( _parse_url title "$tmp" | cut -c 1-251 | tr / _)
    
        curl -s -L -o "${title}.flv" "${BASEURL}video_id=${id}&t=$t"
        >/dev/null 2>&1 ffmpeg -i "${title}.flv" -ar 48000 -ac 2 "${title}.avi"
        rm "${title}.flv"
    done < "$1"
    

    BUT :D

    I don't know why, the URL file seems to be closed after the first encoding with ffmpeg (nothing else to read) ; you can still use MEncoder if no solution/correction :-)

    [ Reply to This | # ]

  • A script to download and convert multiple YouTube videos
    Authored by: LukeR on Mar 01, '08 10:35:24AM
    Yes, in order to get my script to work you need to have wget and ffmpeg installed, but that's easy with Fink or MacPorts. To get better quality of the output avi file, I'd suggest using
    ffmpeg -i "${MOVNAME}"'.flv' -ar 48000 -b 500k -vcodec libxvid -acodec libmp3lame "${MOVNAME}"'.avi'
    line.

    [ Reply to This | # ]
    A script to download and convert multiple YouTube videos
    Authored by: chleuasme on Mar 01, '08 12:45:46PM
    wget, ffmpeg and seq too : therefor I suggested you alternatives built-in solutions (apart ffmpeg ofc)

    and the problem is not to install them: I can't make wget to download the flv (and so your script, to only talk about the download part, doesn't work for me), nor curl without the -L option

    [ Reply to This | # ]

    A script to download and convert multiple YouTube videos
    Authored by: S Barman on Mar 02, '08 06:56:59PM

    There is also the problem of a redirect to the "real" file if the HTTP request returns a 302. Does curl follow the redirect? If not, you have to look for the HTTP return header "Location: " to get the real destination.



    [ Reply to This | # ]
    A script to download and convert multiple YouTube videos
    Authored by: chleuasme on Mar 02, '08 10:10:42PM
    thus the -L option, no?

    And I correct what I said earlier, I can download the flv with wget without option BUT need too both the "video_id" and "t" URL's attributes. The original script still doesn't work for me as no flv is downloaded (Am I the only one here?)

    [ Reply to This | # ]

    A script to download and convert multiple YouTube videos
    Authored by: cwroton on Mar 02, '08 01:32:52PM

    There is a Flash Video Downloader app which allows batch downloads. Easy to use. It's "donationware".

    Also, in Safari, view the Activity window and double click the video item in the list to download the video being viewed. You can tell which item is the video because "video" is part of the title. Also, while the video is loading, you can spot that item because the file size will be increasing.

    No risky Terminal scripts needed.

    ---
    Cy Roton



    [ Reply to This | # ]