php 下載 youtube 的影片,並且取出 mp3 or ogg 音源

如何從 youtube 下載影片並且自動轉出 mp3 or ogg 檔案


解析出來的網址如下:


array(10) {
  [0]=>
  string(422) "url=http://o-o.preferred.ascc-tpe1.v24.lscache3.c.youtube.com/videoplayback?sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Csource%2Cratebypass%2Ccp&fexp=912302%2C906429%2C913105&itag=46&ip=140.0.0.0&signature=3CA6BBF9474841CF105653580E29EB74BB9148FC.325416BE1EBD7407467F642EEFBECCFEBCD5951D&sver=3&ratebypass=yes&source=youtube&expire=1330387565&key=yt1&ipbits=8&cp=U0hSRVNTVV9LT0NOMl9LTVlGOmhZR1cwOUZseVVa&id=e7bceb9ff3396c4f"
  [1]=>
  string(421) "url=http://o-o.preferred.ascc-tpe1.v6.lscache1.c.youtube.com/videoplayback?sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Csource%2Cratebypass%2Ccp&fexp=912302%2C906429%2C913105&itag=37&ip=140.0.0.0&signature=3DD8E663008956A26A1DABFADFC0BD852D0C8CF5.CD9C6458FAB292D8144FA8284ED55479B8065734&sver=3&ratebypass=yes&source=youtube&expire=1330387565&key=yt1&ipbits=8&cp=U0hSRVNTVV9LT0NOMl9LTVlGOmhZR1cwOUZseVVa&id=e7bceb9ff3396c4f"
  [2]=>
  string(422) "url=http://o-o.preferred.ascc-tpe1.v14.lscache3.c.youtube.com/videoplayback?sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Csource%2Cratebypass%2Ccp&fexp=912302%2C906429%2C913105&itag=45&ip=140.0.0.0&signature=5E6D3DBAEA96C7937278BD88FA520F7FAF287A6D.196CA2DBD1F58D5B0C63370FC0CE2097321F53A2&sver=3&ratebypass=yes&source=youtube&expire=1330387565&key=yt1&ipbits=8&cp=U0hSRVNTVV9LT0NOMl9LTVlGOmhZR1cwOUZseVVa&id=e7bceb9ff3396c4f"
  [3]=>
  string(421) "url=http://o-o.preferred.ascc-tpe1.v7.lscache1.c.youtube.com/videoplayback?sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Csource%2Cratebypass%2Ccp&fexp=912302%2C906429%2C913105&itag=22&ip=140.0.0.0&signature=9521DF5BF21112B0C5AC95D4135057DE6EDF6EAD.0DBD9897B1442F30F2436D61459281F266B0E171&sver=3&ratebypass=yes&source=youtube&expire=1330387565&key=yt1&ipbits=8&cp=U0hSRVNTVV9LT0NOMl9LTVlGOmhZR1cwOUZseVVa&id=e7bceb9ff3396c4f"
  [4]=>
  string(422) "url=http://o-o.preferred.ascc-tpe1.v16.lscache4.c.youtube.com/videoplayback?sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Csource%2Cratebypass%2Ccp&fexp=912302%2C906429%2C913105&itag=44&ip=140.0.0.0&signature=708BC1DE44BF36D838A7A343343D448D13070901.B6D3417B3410D1CA4AE590DFD1A2444859711B61&sver=3&ratebypass=yes&source=youtube&expire=1330387565&key=yt1&ipbits=8&cp=U0hSRVNTVV9LT0NOMl9LTVlGOmhZR1cwOUZseVVa&id=e7bceb9ff3396c4f"
  [5]=>
  string(470) "url=http://o-o.preferred.ascc-tpe1.v19.lscache3.c.youtube.com/videoplayback?sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Csource%2Calgorithm%2Cburst%2Cfactor%2Ccp&fexp=912302%2C906429%2C913105&algorithm=throttle-factor&itag=35&ip=140.0.0.0&burst=40&sver=3&signature=5FBC0A0F53CA3B475D3F50404F0A9CCD30CDD972.7C8E3CA2F856B4A907608B862206CE9C6BFC9D63&source=youtube&expire=1330387565&key=yt1&ipbits=8&factor=1.25&cp=U0hSRVNTVV9LT0NOMl9LTVlGOmhZR1cwOUZseVVa&id=e7bceb9ff3396c4f"
  [6]=>
  string(421) "url=http://o-o.preferred.ascc-tpe1.v9.lscache2.c.youtube.com/videoplayback?sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Csource%2Cratebypass%2Ccp&fexp=912302%2C906429%2C913105&itag=43&ip=140.0.0.0&signature=B9811AF3C187D1C938A3B7762CB563895A7179ED.7F50E525FCF19E50EC72C7BFA42F71F08F94D9E6&sver=3&ratebypass=yes&source=youtube&expire=1330387565&key=yt1&ipbits=8&cp=U0hSRVNTVV9LT0NOMl9LTVlGOmhZR1cwOUZseVVa&id=e7bceb9ff3396c4f"
  [7]=>
  string(470) "url=http://o-o.preferred.ascc-tpe1.v22.lscache7.c.youtube.com/videoplayback?sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Csource%2Calgorithm%2Cburst%2Cfactor%2Ccp&fexp=912302%2C906429%2C913105&algorithm=throttle-factor&itag=34&ip=140.0.0.0&burst=40&sver=3&signature=C73DA387F4955ACAAE339AAE67677A62B631EC73.A066F4F997AA4C6D1C90BE3FAE005EF2DE18A753&source=youtube&expire=1330387565&key=yt1&ipbits=8&factor=1.25&cp=U0hSRVNTVV9LT0NOMl9LTVlGOmhZR1cwOUZseVVa&id=e7bceb9ff3396c4f"
  [8]=>
  string(421) "url=http://o-o.preferred.ascc-tpe1.v9.lscache7.c.youtube.com/videoplayback?sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Csource%2Cratebypass%2Ccp&fexp=912302%2C906429%2C913105&itag=18&ip=140.0.0.0&signature=4C1A34563C1B2A42B1C023A2C620C070AC7DEAB7.0CF6081EA6930AA4EC68FD025F6F98B00F902A11&sver=3&ratebypass=yes&source=youtube&expire=1330387565&key=yt1&ipbits=8&cp=U0hSRVNTVV9LT0NOMl9LTVlGOmhZR1cwOUZseVVa&id=e7bceb9ff3396c4f"
  [9]=>
  string(469) "url=http://o-o.preferred.ascc-tpe1.v12.lscache5.c.youtube.com/videoplayback?sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Csource%2Calgorithm%2Cburst%2Cfactor%2Ccp&fexp=912302%2C906429%2C913105&algorithm=throttle-factor&itag=5&ip=140.0.0.0&burst=40&sver=3&signature=D4AB3700B913D6FA884D91AB5A27B7C219DE9912.CF9CB67707CAD08979DF1CFBED4724BC39C78E92&source=youtube&expire=1330387565&key=yt1&ipbits=8&factor=1.25&cp=U0hSRVNTVV9LT0NOMl9LTVlGOmhZR1cwOUZseVVa&id=e7bceb9ff3396c4f"
}

裡面有 ip 這個參數,基本上我試了一下,在 A 這個電腦產生的網址真的沒有辦法在 B 電腦下載(兩邊 IP 不同)

產生出來的連結裡面有個 expire 參數,帶的是 unix time 時間

網址後面帶有 itag 這個參數,這個數值可以提供影片的格式和畫素資訊,請直接看 wiki 的整理:
http://en.wikipedia.org/wiki/YouTube#Quality_and_codecs

$ ffmpeg -i input_name -y -vn -f ogg output_name
$ ./youtube-dl.py --extract-audio --audio-format mp3 http://www.youtube.com/watch?v=57zrn_M5bE8
-y 輸出檔名相同的話直接覆蓋
-vn 只取出音源
-f 輸出格式

另外,上面的 php 程式碼部份,可以選擇取出目前 youtube 上面的影片 webm 格式

ffmpeg 我安裝 0.10 版本才有 webm 的 vp8 格式支援(Ubuntu10.04 server LTS)

sudo apt-get install libfaac-dev libmp3lame-dev

./configuration –enable-pthreads –enable-gpl –enable-libfaac –enable-libmp3lame –enable-nonfree

http://ffmpeg.org/releases/ffmpeg-0.10.tar.gz

試跑的過程中有時候成功有時候失敗,原因不明。
而且學網很慢= =a

<?php
//產生出來的連結不一定第一次點擊就可以使用
//所以網路上的下載方式有些會要求使用者貼上 info 資訊
//目的應該就是先產生第一次點擊
//保證使用者要下載的時候該連結一定可以生效
$file_contents = file_get_contents("http://www.youtube.com/get_video_info?video_id=57zrn_M5bE8");
$file_contents = urldecode(urldecode($file_contents));//至此,產生出來的 http link 可以使用了
preg_match_all('/url=(.+?)id=w{16}/', $file_contents, $p);//$p[0]:包含id ,$p[1]:不含id
$i = 0;
//echo '

preview image

'; foreach( $p[0] as $url ){ $itag = strstr($url, 'itag='); $url = substr($url, strlen("url=")) ; $itag = strstr($itag, '&', true); $itag = strstr($itag, '='); $itag = substr($itag, strlen("=")); $info[$i]["url"] = $url; $info[$i]["itag"] = $itag; $i++; } $max = $info[0]; foreach( $info as $i ){ if( $i["itag"] > $max["itag"] ){ $max = $i; } } //echo $max["itag"]."
n"; //echo $max["url"]."
n"; switch($max["itag"]){ case "46": echo '1080p WebM'; exec('curl "http://www.youtube.com/get_video_info?video_id=57zrn_M5bE8" > /tmp/youtube.tmp'); exec('curl "'.$max["url"].'" > a.webm'); break; case "45": echo '720p WebM'; break; case "44": echo '480p WebM'; break; case "43": echo '360p WebM'; break; case "38": echo '2304p MP4'; break; case "37": echo '1080p MP4'; break; case "22": echo '720p MP4'; break; case "18": echo '360p MP4'; break; case "102": echo '720p WebM 3D'; break; case "101": echo '480p WebM 3D'; break; case "100": echo '360p WebM 3D'; break; case "84": echo '720p MP4 3D'; break; case "85": echo '520p MP4 3D'; break; case "82": echo '360p MP4 3D'; break; case "83": echo '240p MP4 3D'; break; case "35": echo '480p FLV'; break; case "34": echo '360p FLV'; break; case "6": echo '270p FLV'; break; case "5": echo '224p FVL'; break; case "17": echo '3GP'; break; case "13": echo '3GP'; break; default: echo "未知的格式n"; break; } ?>

1 關於 “php 下載 youtube 的影片,並且取出 mp3 or ogg 音源” 的評論

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com 標誌

您的留言將使用 WordPress.com 帳號。 登出 /  變更 )

Google photo

您的留言將使用 Google 帳號。 登出 /  變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 /  變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 /  變更 )

連結到 %s