Laboratorium Komputerowe Progmar
Marcin Załęczny

We are using cookies in the page. If you use the page you agree for the cookies.      Close

Downloading video from Youtube in Ubuntu

First install needed programmes: sudo apt-get install mplayer2 ffmpeg Make sure that you have youtube-dl programm in the newest version: sudo apt-get remove -y youtube-dl
sudo wget https://yt-dl.org/latest/youtube-dl -O /usr/local/bin/youtube-dl
sudo chmod a+x /usr/local/bin/youtube-dl
hash -r

If following command: youtube-dl -g "https://www.youtube.com/watch?v=youtube_video_code" outputs error: ERROR: Signature extraction failed, then probably you haven't the youtube-dl in the latest version. If you have, then the video file could not be downloaded from Youtube (until the author applies needed changes to the programm to support last changes in Youtube service).

Now in /usr/local/bin directory create a file grab.sh with following content:

#!/bin/bash

rm -rf video.mov video.url
rm -rf audio.mov audio.url

for url in $(youtube-dl -g "http://www.youtube.com/watch?v=$1"); do
    if [ ! -r video.mov ]; then
        echo $url > video.url
        cat video.url | xargs curl -L > video.mov
        continue
    fi
    if [ ! -r audio.mov ]; then
        echo $url > audio.url
        cat audio.url | xargs curl -L > audio.mov
        break
    fi
done;

ffmpeg -i video.mov -i audio.mov -vcodec copy -acodec copy "$1.mov"
if [ -r "$1.mov" ]; then
    rm -rf video.mov video.url
    rm -rf audio.mov audio.url
fi

ls -al

Change the file attributes to grant execute privileges: sudo chmod 755 /usr/local/bin/grab.sh

From now you can download the film by issuing command: grab.sh youtube_video_code The script downloads a film in two separate files: video and audio. After download finishes they are mixed together into a single audio-video file.

The script sometimes returns error: moov atom not found
audio.mov: Invalid data found when processing input
This means that one of files mentioned above or both of them are not successfully downloaded. In that case try download them again. In most cases it finishes with success. If you try to download film a few times and you always end with failure then the film can't be downloaded. In the case you have in current directory files audio.mov, video.mov, audio.url and video.url, which you can investigate to get know what is the reason of the failure.


Downloading video from other services in Ubuntu

Install required programme: sudo apt-get install mplayer2 If a video file is served by http protocol, you can download it with command below: mplayer http://url-of-page-containing-video -dumpstream -dumpfile output-filename.flv

In order you to feel comfortable, below there is a short script for downloading videos. You can save it and execute with one parameter - full url of the page containing video. It extracts last part of the url and transforms it into output filename. If the file does not exist in current directory then the file will be downloaded:

#!/bin/bash

url="$1"
fname=${url##http*/}.flv

if [ ! -r "./$fname" ]; then
    mplayer $url -dumpstream -dumpfile $fname
fi