1
0
mirror of https://github.com/Rudloff/alltube.git synced 2024-06-27 07:35:08 +02:00
alltube/classes/VideoDownload.php

130 lines
3.1 KiB
PHP
Raw Normal View History

2014-03-13 20:07:56 +01:00
<?php
/**
* PHP web interface for youtube-dl (http://rg3.github.com/youtube-dl/)
* Main class
*
2014-03-13 20:07:56 +01:00
* PHP Version 5.3.10
*
2014-03-13 20:07:56 +01:00
* @category Youtube-dl
* @package Youtubedl
2015-01-07 10:47:46 +01:00
* @author Pierre Rudloff <contact@rudloff.pro>
2014-03-13 20:07:56 +01:00
* @license GNU General Public License http://www.gnu.org/licenses/gpl.html
* @link http://rudloff.pro
* */
2015-10-29 20:43:43 +01:00
namespace Alltube;
2014-03-13 20:07:56 +01:00
/**
* PHP web interface for youtube-dl (http://rg3.github.com/youtube-dl/)
* Main class
*
2014-03-13 20:07:56 +01:00
* PHP Version 5.3.10
*
2014-03-13 20:07:56 +01:00
* @category Youtube-dl
* @package Youtubedl
2015-01-07 10:47:46 +01:00
* @author Pierre Rudloff <contact@rudloff.pro>
2014-03-13 20:07:56 +01:00
* @license GNU General Public License http://www.gnu.org/licenses/gpl.html
* @link http://rudloff.pro
* */
Class VideoDownload
{
/**
* Get the user agent used youtube-dl
*
2014-03-13 20:07:56 +01:00
* @return string UA
* */
2015-04-13 00:49:50 +02:00
static function getUA()
2014-03-13 20:07:56 +01:00
{
exec(
PYTHON.' '.YOUTUBE_DL.' --dump-user-agent',
$version
2014-03-13 20:07:56 +01:00
);
return $version[0];
}
2014-03-13 20:07:56 +01:00
/**
* List all extractors
*
2014-03-13 20:07:56 +01:00
* @return array Extractors
* */
2015-04-13 00:49:50 +02:00
static function listExtractors()
2014-03-13 20:07:56 +01:00
{
exec(
PYTHON.' '.YOUTUBE_DL.' --list-extractors',
$extractors
2014-03-13 20:07:56 +01:00
);
return $extractors;
}
2014-03-13 20:07:56 +01:00
/**
* Get filename of video
*
2014-03-13 20:07:56 +01:00
* @param string $url URL of page
* @param string $format Format to use for the video
*
2014-03-13 20:07:56 +01:00
* @return string Filename
* */
2015-04-13 00:49:50 +02:00
static function getFilename($url, $format=null)
2014-03-13 20:07:56 +01:00
{
$cmd=PYTHON.' '.YOUTUBE_DL;
2014-03-13 20:07:56 +01:00
if (isset($format)) {
$cmd .= ' -f '.escapeshellarg($format);
}
$cmd .=' --get-filename '.escapeshellarg($url)." 2>&1";
exec(
$cmd,
$filename
);
return end($filename);
}
2014-03-13 20:07:56 +01:00
/**
* Get all information about a video
*
2014-03-13 20:07:56 +01:00
* @param string $url URL of page
* @param string $format Format to use for the video
*
2014-03-13 20:07:56 +01:00
* @return string JSON
* */
2015-04-13 00:49:50 +02:00
static function getJSON($url, $format=null)
2014-03-13 20:07:56 +01:00
{
$cmd=PYTHON.' '.YOUTUBE_DL.' '.PARAMS;
2014-03-13 20:07:56 +01:00
if (isset($format)) {
$cmd .= ' -f '.escapeshellarg($format);
}
$cmd .=' --dump-json '.escapeshellarg($url)." 2>&1";
2014-03-13 20:07:56 +01:00
exec(
$cmd, $result, $code
2014-03-13 20:07:56 +01:00
);
2014-03-25 19:09:40 +01:00
if ($code>0) {
2015-10-29 20:43:43 +01:00
throw new \Exception(implode(PHP_EOL, $result));
2014-03-18 15:08:16 +01:00
} else {
return json_decode($result[0]);
2014-03-18 15:08:16 +01:00
}
2014-03-13 20:07:56 +01:00
}
/**
* Get URL of video from URL of page
*
2014-03-13 20:07:56 +01:00
* @param string $url URL of page
* @param string $format Format to use for the video
*
2014-03-13 20:07:56 +01:00
* @return string URL of video
* */
2015-04-13 00:49:50 +02:00
static function getURL($url, $format=null)
2014-03-13 20:07:56 +01:00
{
$cmd=PYTHON.' '.YOUTUBE_DL.' '.PARAMS;
2014-03-13 20:07:56 +01:00
if (isset($format)) {
$cmd .= ' -f '.escapeshellarg($format);
}
$cmd .=' -g '.escapeshellarg($url)." 2>&1";
exec(
$cmd, $result, $code
2014-03-13 20:07:56 +01:00
);
if ($code>0) {
2015-10-29 20:43:43 +01:00
throw new \Exception(implode(PHP_EOL, $result));
2014-03-13 20:07:56 +01:00
} else {
return array('success'=>true, 'url'=>end($result));
2014-03-13 20:07:56 +01:00
}
2014-03-13 20:07:56 +01:00
}
}