Created
March 23, 2012 05:28
-
-
Save sartas/2167250 to your computer and use it in GitHub Desktop.
video_loader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/php5 | |
| <?php | |
| function save_to() { | |
| $save_to = getenv('HOME') . '/flash/video/'; | |
| return $save_to; | |
| } | |
| function links_from() { | |
| $links_from = getenv('HOME') . '/flash/video.txt'; | |
| return $links_from; | |
| } | |
| //http://rutube.ru/tracks/5444973.html | |
| function rutube($url) { | |
| $html = get_html($url); | |
| $hash = get_hash($html); | |
| if (empty($hash)) { | |
| echo "\n\n" . 'Ролик не найден ' . $url . "\n\n"; | |
| return 'not_found'; | |
| } | |
| $title = get_title($html); | |
| $rtmp_url = get_rtmp_url($hash); | |
| $rtmp_args = parse_rtmp_url($rtmp_url); | |
| echo "\n\n" . 'Скачивание: ' . $title . "\n\n\n"; | |
| $rtmp_args['video_file'] = save_to() . $title . '.flv'; | |
| return download_rtmp($rtmp_args); | |
| } | |
| function download_rtmp($rtmp_args) { | |
| $args = 'rtmpdump --resume ' . | |
| '--rtmp "' . $rtmp_args['rtmp'] . '" ' . | |
| '--app "' . $rtmp_args['app'] . '" ' . | |
| '--swfUrl "http://rutube.ru/player.swf" ' . | |
| '--playpath "' . $rtmp_args['playpath'] . '" ' . | |
| '--flv "' . $rtmp_args['video_file'] . '" '; | |
| system($args, $result); | |
| //файл поврежден, докачать не получится | |
| if ($result == 1) { | |
| return $rtmp_args['video_file']; | |
| } | |
| return $result; | |
| } | |
| function get_html($url) { | |
| $html_koi8 = file_get_contents($url); | |
| $html = iconv('KOI-8', 'UTF-8', $html_koi8); | |
| return $html; | |
| } | |
| function get_hash($html) { | |
| //<meta property="og:video" content="http://video.rutube.ru/b34feab52e54134d67abe1b298961b32" /> | |
| preg_match('#<meta property=\"og:video\" content=\"http:\/\/video\.rutube\.ru\/(.*)\" \/>#u', $html, $matches); | |
| if (isset($matches[1]) && !empty($matches[1])) | |
| return $matches[1]; | |
| else | |
| return false; | |
| } | |
| function get_title($html) { | |
| //<meta property="og:title" content="Корона вины 21 серия[рус.озв Jam & Eladiel]2011" /> | |
| preg_match('#<meta property=\"og:title\" content=\"(.*)\" \/>#u', $html, $matches); | |
| if (isset($matches[1]) && !empty($matches[1])) { | |
| $matches[1] = str_replace('/', '-', $matches[1]); | |
| return $matches[1]; | |
| } | |
| else | |
| return false; | |
| } | |
| function get_rtmp_url($hash) { | |
| $xml_url = 'http://bl.rutube.ru/' . $hash . '.xml'; | |
| $xml = file_get_contents($xml_url); | |
| // <![CDATA[rtmp://video-13-10.rutube.ru/rutube_vod_2/mp4:n1vol2/movies/b3/4f/b34feab52e54134d67abe1b298961b32.mp4?e=1332159475&s=592d0d79594dbe554b49c7e5addd2720]]> | |
| preg_match('#<\!\[CDATA\[(.*)\]\]>#u', $xml, $matches); | |
| if (isset($matches[1]) && !empty($matches[1])) | |
| return $matches[1]; | |
| else | |
| return false; | |
| } | |
| function parse_rtmp_url($rtmp_url) { | |
| // rtmp://video-13-10.rutube.ru/rutube_vod_2/mp4:n1vol2/movies/b3/4f/b34feab52e54134d67abe1b298961b32.mp4?e=1332159853&s=7c2f3246d6f6efdc3a06e27cdc274b75 | |
| preg_match('#(rtmp:\/\/.*\.rutube\.ru\/)(.*\/)(mp4.*)#u', $rtmp_url, $matches); | |
| $rtmp_args = array(); | |
| $rtmp_args['rtmp'] = $matches[1]; | |
| $rtmp_args['app'] = $matches[2]; | |
| $rtmp_args['playpath'] = $matches[3]; | |
| return $rtmp_args; | |
| } | |
| function kiwi($link) { | |
| $html = file_get_contents($link); | |
| //flashvars="config=http://p.kiwi.kz/static/player2/video.txt&url=http://farm.kiwi.kz/v/3kn0wzf8pc1p/%3Fsecret%3DE%2BeSooxmxQzxGiayQZvzeg%3D%3D&poster=http://im8.ass | |
| preg_match('#url\=(http\:\/\/farm\.kiwi\.kz.*?)\&\;#u', $html, $matches); | |
| $title = kiwi_title($html); | |
| return wget($matches[1], $title); | |
| } | |
| function kiwi_title($html) { | |
| //<meta name="og:title" content="Bakuman/Бакуман 2 сезон 22 серия [рус.озв Ancord] 47 серия" > | |
| preg_match('#<meta name=\"og:title\" content=\"(.*)\" >#u', $html, $matches); | |
| if (isset($matches[1]) && !empty($matches[1])) { | |
| $matches[1] = str_replace('/', '-', $matches[1]); | |
| return $matches[1]; | |
| } | |
| else | |
| return false; | |
| } | |
| function wget($url, $title) { | |
| echo "\n\n" . 'Скачивание: ' . $title . "\n\n\n"; | |
| $video_file = save_to() . $title . '.flv'; | |
| system('wget -t 3 -c "' . $url . '" -O "' . $video_file . '"', $result); | |
| return $result; | |
| } | |
| function process_links() { | |
| $file = links_from(); | |
| if (!is_file($file)) { | |
| echo 'Необходимо вставить ссылки в файл ' . $file; | |
| exit(); | |
| } | |
| $links = file($file); | |
| if (empty($links)) { | |
| echo "\n\nВсе ролики скачаны \n\n"; | |
| exit(); | |
| } | |
| $link = $links[0]; | |
| $link = trim($link, "\n"); | |
| if (strpos($link, 'http') !== 0) { | |
| unset($links[0]); | |
| file_put_contents($file, $links, LOCK_EX); | |
| process_links(); | |
| } elseif (strpos($link, 'http://rutube.ru') === 0) { | |
| $result = rutube($link); | |
| } elseif (strpos($link, 'http://kiwi.kz') === 0) { | |
| $result = kiwi($link); | |
| } | |
| if ($result === 0) { | |
| $links = file($file); | |
| $key = array_search($link, $links); | |
| unset($links[$key]); | |
| file_put_contents($file, $links, LOCK_EX); | |
| } elseif ($result == 'not_found') { | |
| $links = file($file); | |
| $key = array_search($link, $links); | |
| unset($links[$key]); | |
| file_put_contents($file, $links, LOCK_EX); | |
| } | |
| //закачку можно продолжить | |
| elseif ($result === 2) { | |
| } | |
| //файл поврежден, удаляем его | |
| else { | |
| if (is_file($result)) { | |
| unlink($result); | |
| } | |
| } | |
| process_links(); | |
| } | |
| process_links(); | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment