youtubeのダウンロード&動画変換スクリプト

最近youtubeのリストは溜るもののスクリプトを複数に分割させていたので変換するのが大儀になっていた。せっかく家に籠っているのだからと、一つのスクリプトに直した。サイズは大きくないので下に記載します。

以下、list.txtの例

http://www.youtube.com/watch?v=O6oc0UM3dHM
http://www.youtube.com/watch?v=DxDUoj7X5l0
http://www.youtube.com/watch?v=2KImZnbQGCo
...
    • 後は該当スクリプトを実行するだけでlist.txtを読み取る。ダウンロードしたファイルリストはorigというディレクトリの中に、aviにコンバートされたファイルは320x240の解像度でaviというディレクトリに生成される。生成された動画ファイルはYoutubeサイトに記載されたタイトルにリネームされる。
  • 以下、スクリプトの内容
#!/usr/bin/perl

use LWP::Simple;

open(PIPE,"cat list.txt|");

while(<PIPE>)
{
    chop();

    $cmd = "../youtube-dl $_";
    print "$cmd\n";
    system $cmd;
}

close(PIPE);

mkdir "orig";

system "mv *.flv orig";

while (<orig/*.flv>)
{
    $infile= $_;
    $outfile = $infile;
    $outfile =~ s/\.flv/\.avi/;

    $cmd = "ffmpeg -i $infile -s 320x240 $outfile";
    system $cmd;
}

system "mv orig/*.avi .";

while(<*.avi>) {
    if(/(\S+).avi/){
	$id= $1;
    }
    print "$id\n";

    $content = get("http://www.youtube.com/watch?v=$id");

    if($content =~ /\>YouTube - (.+)\</){
	$title=$1;
	$title=~ tr/ /_/;
	print "title=$title\n";
    }

    $name1 = "$id.avi";
    $name2 = "$title-$id.avi";
    print "mv $name1 $name2\n";

    rename($name1,$name2);
}

mkdir "avi";

system "mv *.avi avi";

I wrote the script that downloads youtube file using youtube-dl and converts to mpeg that is visible for zaurus. If you are reluctant to download and convert youtube files manually, please refere this.