2014-03-13 19:07:56 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2016-09-07 22:28:28 +00:00
|
|
|
* VideoDownload class.
|
2016-08-01 11:29:13 +00:00
|
|
|
*/
|
2016-12-05 12:12:27 +00:00
|
|
|
|
2015-10-29 19:43:43 +00:00
|
|
|
namespace Alltube;
|
2016-03-29 23:49:08 +00:00
|
|
|
|
2018-02-05 15:48:58 +00:00
|
|
|
use Exception;
|
|
|
|
use stdClass;
|
2017-12-23 13:17:29 +00:00
|
|
|
use Symfony\Component\Process\Process;
|
2016-03-31 22:42:28 +00:00
|
|
|
|
2014-03-13 19:07:56 +00:00
|
|
|
/**
|
2016-09-07 22:28:28 +00:00
|
|
|
* Extract info about videos.
|
2016-08-01 11:29:13 +00:00
|
|
|
*/
|
2016-03-29 23:49:08 +00:00
|
|
|
class VideoDownload
|
2014-03-13 19:07:56 +00:00
|
|
|
{
|
2016-10-14 00:40:33 +00:00
|
|
|
/**
|
2016-10-14 00:40:57 +00:00
|
|
|
* Config instance.
|
|
|
|
*
|
2016-10-14 00:40:33 +00:00
|
|
|
* @var Config
|
|
|
|
*/
|
2016-10-10 19:32:07 +00:00
|
|
|
private $config;
|
2016-10-14 00:40:33 +00:00
|
|
|
|
2016-08-01 11:29:13 +00:00
|
|
|
/**
|
2016-09-07 22:28:28 +00:00
|
|
|
* VideoDownload constructor.
|
2017-12-19 14:20:52 +00:00
|
|
|
*
|
2017-12-24 00:12:47 +00:00
|
|
|
* @param Config $config Config instance.
|
|
|
|
*
|
2018-02-05 15:48:58 +00:00
|
|
|
* @throws Exception If youtube-dl is missing
|
|
|
|
* @throws Exception If Python is missing
|
2016-08-01 11:29:13 +00:00
|
|
|
*/
|
2016-10-18 08:03:50 +00:00
|
|
|
public function __construct(Config $config = null)
|
2016-04-08 17:06:41 +00:00
|
|
|
{
|
2016-10-18 08:03:50 +00:00
|
|
|
if (isset($config)) {
|
|
|
|
$this->config = $config;
|
|
|
|
} else {
|
|
|
|
$this->config = Config::getInstance();
|
|
|
|
}
|
2018-01-26 10:37:43 +00:00
|
|
|
/*
|
|
|
|
We don't translate these exceptions because they always occur before Slim can catch them
|
|
|
|
so they will always go to the logs.
|
|
|
|
*/
|
2016-10-18 07:27:28 +00:00
|
|
|
if (!is_file($this->config->youtubedl)) {
|
2018-02-05 15:48:58 +00:00
|
|
|
throw new Exception("Can't find youtube-dl at ".$this->config->youtubedl);
|
2017-11-12 14:14:59 +00:00
|
|
|
} elseif (!$this->checkCommand([$this->config->python, '--version'])) {
|
2018-02-05 15:48:58 +00:00
|
|
|
throw new Exception("Can't find Python at ".$this->config->python);
|
2016-10-18 07:27:28 +00:00
|
|
|
}
|
2017-12-23 13:17:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-23 13:37:29 +00:00
|
|
|
* Return a youtube-dl process with the specified arguments.
|
2017-12-23 13:17:29 +00:00
|
|
|
*
|
|
|
|
* @param string[] $arguments Arguments
|
|
|
|
*
|
|
|
|
* @return Process
|
|
|
|
*/
|
|
|
|
private function getProcess(array $arguments)
|
|
|
|
{
|
|
|
|
return new Process(
|
2016-04-08 17:37:59 +00:00
|
|
|
array_merge(
|
2016-09-07 22:28:28 +00:00
|
|
|
[$this->config->python, $this->config->youtubedl],
|
2017-12-23 13:17:29 +00:00
|
|
|
$this->config->params,
|
|
|
|
$arguments
|
2016-04-08 17:37:59 +00:00
|
|
|
)
|
|
|
|
);
|
2016-04-08 17:06:41 +00:00
|
|
|
}
|
|
|
|
|
2014-03-13 19:07:56 +00:00
|
|
|
/**
|
2016-09-07 22:28:28 +00:00
|
|
|
* List all extractors.
|
2015-09-04 20:45:55 +00:00
|
|
|
*
|
2016-08-01 11:29:13 +00:00
|
|
|
* @return string[] Extractors
|
2014-03-13 19:07:56 +00:00
|
|
|
* */
|
2016-04-08 17:06:41 +00:00
|
|
|
public function listExtractors()
|
2014-03-13 19:07:56 +00:00
|
|
|
{
|
2017-11-11 12:58:55 +00:00
|
|
|
return explode("\n", trim($this->getProp(null, null, 'list-extractors')));
|
2014-03-13 19:07:56 +00:00
|
|
|
}
|
2015-09-04 20:45:55 +00:00
|
|
|
|
2016-10-14 00:40:33 +00:00
|
|
|
/**
|
2016-10-14 00:40:57 +00:00
|
|
|
* Get a property from youtube-dl.
|
|
|
|
*
|
2016-10-20 21:01:31 +00:00
|
|
|
* @param string $url URL to parse
|
|
|
|
* @param string $format Format
|
|
|
|
* @param string $prop Property
|
|
|
|
* @param string $password Video password
|
2016-10-14 00:40:57 +00:00
|
|
|
*
|
2017-12-19 14:20:52 +00:00
|
|
|
* @throws PasswordException If the video is protected by a password and no password was specified
|
2018-02-05 15:48:58 +00:00
|
|
|
* @throws Exception If the password is wrong
|
|
|
|
* @throws Exception If youtube-dl returns an error
|
2017-12-19 14:20:52 +00:00
|
|
|
*
|
2016-10-14 00:40:33 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-10-20 21:01:31 +00:00
|
|
|
private function getProp($url, $format = null, $prop = 'dump-json', $password = null)
|
2014-03-13 19:07:56 +00:00
|
|
|
{
|
2017-12-23 13:17:29 +00:00
|
|
|
$arguments = [
|
|
|
|
'--'.$prop,
|
2017-12-23 13:37:29 +00:00
|
|
|
$url,
|
2017-12-23 13:17:29 +00:00
|
|
|
];
|
2014-03-13 19:07:56 +00:00
|
|
|
if (isset($format)) {
|
2017-12-23 13:17:29 +00:00
|
|
|
$arguments[] = '-f '.$format;
|
2014-03-13 19:07:56 +00:00
|
|
|
}
|
2016-10-20 21:01:31 +00:00
|
|
|
if (isset($password)) {
|
2017-12-23 13:17:29 +00:00
|
|
|
$arguments[] = '--video-password';
|
|
|
|
$arguments[] = $password;
|
2016-10-20 21:01:31 +00:00
|
|
|
}
|
2017-11-01 22:02:19 +00:00
|
|
|
|
2017-12-23 13:17:29 +00:00
|
|
|
$process = $this->getProcess($arguments);
|
2017-11-01 22:02:19 +00:00
|
|
|
//This is needed by the openload extractor because it runs PhantomJS
|
2018-01-25 15:08:00 +00:00
|
|
|
$process->setEnv(['PATH'=>$this->config->phantomjsDir]);
|
2017-12-23 16:41:39 +00:00
|
|
|
$process->inheritEnvironmentVariables();
|
2016-03-31 22:42:28 +00:00
|
|
|
$process->run();
|
|
|
|
if (!$process->isSuccessful()) {
|
2016-10-20 21:01:31 +00:00
|
|
|
$errorOutput = trim($process->getErrorOutput());
|
2018-02-06 18:20:56 +00:00
|
|
|
$exitCode = $process->getExitCode();
|
2016-10-20 21:01:31 +00:00
|
|
|
if ($errorOutput == 'ERROR: This video is protected by a password, use the --video-password option') {
|
2018-02-06 18:20:56 +00:00
|
|
|
throw new PasswordException($errorOutput, $exitCode);
|
2016-10-20 21:01:31 +00:00
|
|
|
} elseif (substr($errorOutput, 0, 21) == 'ERROR: Wrong password') {
|
2018-02-06 18:20:56 +00:00
|
|
|
throw new Exception(_('Wrong password'), $exitCode);
|
2016-10-20 21:01:31 +00:00
|
|
|
} else {
|
2018-02-06 18:20:56 +00:00
|
|
|
throw new Exception($errorOutput, $exitCode);
|
2016-10-20 21:01:31 +00:00
|
|
|
}
|
2014-03-18 14:08:16 +00:00
|
|
|
} else {
|
2017-04-24 17:16:38 +00:00
|
|
|
return trim($process->getOutput());
|
2014-03-18 14:08:16 +00:00
|
|
|
}
|
2014-03-13 19:07:56 +00:00
|
|
|
}
|
|
|
|
|
2016-10-13 14:40:19 +00:00
|
|
|
/**
|
|
|
|
* Get all information about a video.
|
|
|
|
*
|
2016-10-20 21:01:31 +00:00
|
|
|
* @param string $url URL of page
|
|
|
|
* @param string $format Format to use for the video
|
|
|
|
* @param string $password Video password
|
2016-10-13 14:40:19 +00:00
|
|
|
*
|
|
|
|
* @return object Decoded JSON
|
|
|
|
* */
|
2016-10-20 21:01:31 +00:00
|
|
|
public function getJSON($url, $format = null, $password = null)
|
2016-10-13 14:40:19 +00:00
|
|
|
{
|
2017-04-24 23:53:38 +00:00
|
|
|
return json_decode($this->getProp($url, $format, 'dump-single-json', $password));
|
2016-10-13 14:40:19 +00:00
|
|
|
}
|
|
|
|
|
2014-03-13 19:07:56 +00:00
|
|
|
/**
|
2016-09-07 22:28:28 +00:00
|
|
|
* Get URL of video from URL of page.
|
2015-09-04 20:45:55 +00:00
|
|
|
*
|
2017-04-24 22:40:24 +00:00
|
|
|
* It generally returns only one URL.
|
|
|
|
* But it can return two URLs when multiple formats are specified
|
|
|
|
* (eg. bestvideo+bestaudio).
|
|
|
|
*
|
2016-10-20 21:01:31 +00:00
|
|
|
* @param string $url URL of page
|
|
|
|
* @param string $format Format to use for the video
|
|
|
|
* @param string $password Video password
|
2015-09-04 20:45:55 +00:00
|
|
|
*
|
2017-04-25 12:55:21 +00:00
|
|
|
* @return string[] URLs of video
|
2014-03-13 19:07:56 +00:00
|
|
|
* */
|
2016-10-20 21:01:31 +00:00
|
|
|
public function getURL($url, $format = null, $password = null)
|
2014-03-13 19:07:56 +00:00
|
|
|
{
|
2018-05-23 19:50:06 +00:00
|
|
|
$urls = explode("\n", $this->getProp($url, $format, 'get-url', $password));
|
|
|
|
|
|
|
|
if (empty($urls[0])) {
|
2018-05-23 20:38:15 +00:00
|
|
|
throw new EmptyUrlException(_('youtube-dl returned an empty URL.'));
|
2018-05-23 19:50:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $urls;
|
2014-03-13 19:07:56 +00:00
|
|
|
}
|
2016-07-29 22:15:17 +00:00
|
|
|
|
2016-08-01 11:29:13 +00:00
|
|
|
/**
|
2016-09-07 22:28:28 +00:00
|
|
|
* Get filename of video file from URL of page.
|
2016-08-01 11:29:13 +00:00
|
|
|
*
|
2016-10-20 21:01:31 +00:00
|
|
|
* @param string $url URL of page
|
|
|
|
* @param string $format Format to use for the video
|
|
|
|
* @param string $password Video password
|
2016-08-01 11:29:13 +00:00
|
|
|
*
|
|
|
|
* @return string Filename of extracted video
|
|
|
|
* */
|
2016-10-20 21:01:31 +00:00
|
|
|
public function getFilename($url, $format = null, $password = null)
|
2016-07-29 22:15:17 +00:00
|
|
|
{
|
2016-10-20 21:01:31 +00:00
|
|
|
return trim($this->getProp($url, $format, 'get-filename', $password));
|
2016-07-29 22:15:17 +00:00
|
|
|
}
|
|
|
|
|
2016-08-01 11:29:13 +00:00
|
|
|
/**
|
2017-04-24 22:41:49 +00:00
|
|
|
* Get filename of video with the specified extension.
|
2016-08-01 11:29:13 +00:00
|
|
|
*
|
2017-04-24 22:40:24 +00:00
|
|
|
* @param string $extension New file extension
|
|
|
|
* @param string $url URL of page
|
|
|
|
* @param string $format Format to use for the video
|
|
|
|
* @param string $password Video password
|
2016-08-01 11:29:13 +00:00
|
|
|
*
|
2017-04-24 22:40:24 +00:00
|
|
|
* @return string Filename of extracted video with specified extension
|
|
|
|
*/
|
|
|
|
public function getFileNameWithExtension($extension, $url, $format = null, $password = null)
|
2016-07-29 22:15:17 +00:00
|
|
|
{
|
|
|
|
return html_entity_decode(
|
|
|
|
pathinfo(
|
2016-10-20 21:13:37 +00:00
|
|
|
$this->getFilename($url, $format, $password),
|
2016-07-29 22:15:17 +00:00
|
|
|
PATHINFO_FILENAME
|
2017-04-24 22:40:24 +00:00
|
|
|
).'.'.$extension,
|
2016-07-29 22:15:17 +00:00
|
|
|
ENT_COMPAT,
|
|
|
|
'ISO-8859-1'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-04-24 22:40:24 +00:00
|
|
|
/**
|
|
|
|
* Get filename of audio from URL of page.
|
|
|
|
*
|
|
|
|
* @param string $url URL of page
|
|
|
|
* @param string $format Format to use for the video
|
|
|
|
* @param string $password Video password
|
|
|
|
*
|
|
|
|
* @return string Filename of converted audio file
|
|
|
|
* */
|
|
|
|
public function getAudioFilename($url, $format = null, $password = null)
|
|
|
|
{
|
|
|
|
return $this->getFileNameWithExtension('mp3', $url, $format, $password);
|
|
|
|
}
|
|
|
|
|
2016-10-15 14:18:04 +00:00
|
|
|
/**
|
2017-12-23 13:17:29 +00:00
|
|
|
* Return arguments used to run rtmp for a specific video.
|
2016-10-15 14:18:04 +00:00
|
|
|
*
|
2017-12-23 13:17:29 +00:00
|
|
|
* @param object $video Video object returned by youtube-dl
|
2016-10-15 14:18:04 +00:00
|
|
|
*
|
2017-12-23 14:14:43 +00:00
|
|
|
* @return array Arguments
|
2016-10-15 14:18:04 +00:00
|
|
|
*/
|
2018-02-05 15:48:58 +00:00
|
|
|
private function getRtmpArguments(stdClass $video)
|
2016-10-15 14:18:04 +00:00
|
|
|
{
|
2017-12-23 15:04:55 +00:00
|
|
|
$arguments = [];
|
|
|
|
|
2018-07-03 18:09:45 +00:00
|
|
|
if ($video->protocol == 'rtmp') {
|
|
|
|
foreach ([
|
|
|
|
'url' => '-rtmp_tcurl',
|
|
|
|
'webpage_url' => '-rtmp_pageurl',
|
|
|
|
'player_url' => '-rtmp_swfverify',
|
|
|
|
'flash_version' => '-rtmp_flashver',
|
|
|
|
'play_path' => '-rtmp_playpath',
|
|
|
|
'app' => '-rtmp_app',
|
|
|
|
] as $property => $option) {
|
|
|
|
if (isset($video->{$property})) {
|
|
|
|
$arguments[] = $option;
|
|
|
|
$arguments[] = $video->{$property};
|
|
|
|
}
|
2016-10-15 14:18:04 +00:00
|
|
|
}
|
2016-10-15 14:20:54 +00:00
|
|
|
|
2018-07-03 18:09:45 +00:00
|
|
|
if (isset($video->rtmp_conn)) {
|
|
|
|
foreach ($video->rtmp_conn as $conn) {
|
|
|
|
$arguments[] = '-rtmp_conn';
|
|
|
|
$arguments[] = $conn;
|
|
|
|
}
|
2016-10-14 17:01:51 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-14 17:02:14 +00:00
|
|
|
|
2017-12-23 14:14:43 +00:00
|
|
|
return $arguments;
|
2016-10-14 17:01:51 +00:00
|
|
|
}
|
|
|
|
|
2017-05-13 22:52:59 +00:00
|
|
|
/**
|
2017-05-13 22:54:47 +00:00
|
|
|
* Check if a command runs successfully.
|
2017-05-13 22:52:59 +00:00
|
|
|
*
|
|
|
|
* @param array $command Command and arguments
|
|
|
|
*
|
|
|
|
* @return bool False if the command returns an error, true otherwise
|
|
|
|
*/
|
|
|
|
private function checkCommand(array $command)
|
|
|
|
{
|
2017-12-23 13:17:29 +00:00
|
|
|
$process = new Process($command);
|
2017-05-13 22:52:59 +00:00
|
|
|
$process->run();
|
|
|
|
|
|
|
|
return $process->isSuccessful();
|
|
|
|
}
|
|
|
|
|
2016-10-14 17:01:51 +00:00
|
|
|
/**
|
2018-01-24 22:30:24 +00:00
|
|
|
* Get a process that runs avconv in order to convert a video.
|
2016-10-14 17:02:14 +00:00
|
|
|
*
|
2018-01-24 22:30:24 +00:00
|
|
|
* @param object $video Video object returned by youtube-dl
|
|
|
|
* @param int $audioBitrate Audio bitrate of the converted file
|
|
|
|
* @param string $filetype Filetype of the converted file
|
|
|
|
* @param bool $audioOnly True to return an audio-only file
|
2018-07-03 17:47:35 +00:00
|
|
|
* @param string $from Start the conversion at this time
|
|
|
|
* @param string $to End the conversion at this time
|
2016-10-14 17:02:14 +00:00
|
|
|
*
|
2018-02-05 15:48:58 +00:00
|
|
|
* @throws Exception If avconv/ffmpeg is missing
|
2017-12-19 14:20:52 +00:00
|
|
|
*
|
2017-12-24 00:12:47 +00:00
|
|
|
* @return Process Process
|
2016-10-14 17:01:51 +00:00
|
|
|
*/
|
2018-07-03 17:47:35 +00:00
|
|
|
private function getAvconvProcess(
|
|
|
|
stdClass $video,
|
|
|
|
$audioBitrate,
|
|
|
|
$filetype = 'mp3',
|
|
|
|
$audioOnly = true,
|
|
|
|
$from = null,
|
|
|
|
$to = null
|
|
|
|
) {
|
2017-05-13 22:52:59 +00:00
|
|
|
if (!$this->checkCommand([$this->config->avconv, '-version'])) {
|
2018-02-21 17:55:12 +00:00
|
|
|
throw new Exception(_('Can\'t find avconv or ffmpeg at ').$this->config->avconv.'.');
|
2016-10-14 17:01:51 +00:00
|
|
|
}
|
2016-10-14 17:02:14 +00:00
|
|
|
|
2018-07-03 17:47:35 +00:00
|
|
|
$durationRegex = '/(\d+:)?(\d+:)?(\d+)/';
|
|
|
|
|
2018-07-03 18:09:45 +00:00
|
|
|
$afterArguments = [];
|
2017-12-23 14:14:43 +00:00
|
|
|
|
2018-01-24 22:30:24 +00:00
|
|
|
if ($audioOnly) {
|
2018-07-03 18:09:45 +00:00
|
|
|
$afterArguments[] = '-vn';
|
2018-07-03 17:47:35 +00:00
|
|
|
}
|
|
|
|
|
2018-07-03 18:09:45 +00:00
|
|
|
if (!empty($from)) {
|
2018-07-03 17:47:35 +00:00
|
|
|
if (!preg_match($durationRegex, $from)) {
|
|
|
|
throw new Exception(_('Invalid start time: ').$from.'.');
|
|
|
|
}
|
|
|
|
$afterArguments[] = '-ss';
|
|
|
|
$afterArguments[] = $from;
|
|
|
|
}
|
2018-07-03 18:09:45 +00:00
|
|
|
if (!empty($to)) {
|
2018-07-03 17:47:35 +00:00
|
|
|
if (!preg_match($durationRegex, $to)) {
|
|
|
|
throw new Exception(_('Invalid end time: ').$to.'.');
|
|
|
|
}
|
|
|
|
$afterArguments[] = '-to';
|
|
|
|
$afterArguments[] = $to;
|
2018-01-24 22:30:24 +00:00
|
|
|
}
|
|
|
|
|
2017-12-23 14:14:43 +00:00
|
|
|
$arguments = array_merge(
|
|
|
|
[
|
|
|
|
$this->config->avconv,
|
|
|
|
'-v', $this->config->avconvVerbosity,
|
|
|
|
],
|
2018-07-03 18:09:45 +00:00
|
|
|
$this->getRtmpArguments($video),
|
2017-12-23 14:14:43 +00:00
|
|
|
[
|
|
|
|
'-i', $video->url,
|
2018-01-24 22:30:24 +00:00
|
|
|
'-f', $filetype,
|
|
|
|
'-b:a', $audioBitrate.'k',
|
|
|
|
],
|
2018-07-03 17:47:35 +00:00
|
|
|
$afterArguments,
|
2018-01-24 22:30:24 +00:00
|
|
|
[
|
2017-12-23 14:14:43 +00:00
|
|
|
'pipe:1',
|
|
|
|
]
|
|
|
|
);
|
|
|
|
if ($video->url != '-') {
|
2017-12-09 22:56:34 +00:00
|
|
|
//Vimeo needs a correct user-agent
|
|
|
|
$arguments[] = '-user_agent';
|
|
|
|
$arguments[] = $this->getProp(null, null, 'dump-user-agent');
|
|
|
|
}
|
|
|
|
|
2017-12-23 13:17:29 +00:00
|
|
|
return new Process($arguments);
|
2016-10-14 17:01:51 +00:00
|
|
|
}
|
|
|
|
|
2016-08-01 11:29:13 +00:00
|
|
|
/**
|
2016-09-07 22:28:28 +00:00
|
|
|
* Get audio stream of converted video.
|
2016-08-01 11:29:13 +00:00
|
|
|
*
|
2016-10-20 21:13:37 +00:00
|
|
|
* @param string $url URL of page
|
|
|
|
* @param string $format Format to use for the video
|
|
|
|
* @param string $password Video password
|
2018-07-03 17:47:35 +00:00
|
|
|
* @param string $from Start the conversion at this time
|
|
|
|
* @param string $to End the conversion at this time
|
2016-08-01 11:29:13 +00:00
|
|
|
*
|
2018-02-05 15:48:58 +00:00
|
|
|
* @throws Exception If your try to convert and M3U8 video
|
|
|
|
* @throws Exception If the popen stream was not created correctly
|
2017-12-19 14:20:52 +00:00
|
|
|
*
|
2017-12-05 15:06:57 +00:00
|
|
|
* @return resource popen stream
|
2016-08-01 11:29:13 +00:00
|
|
|
*/
|
2018-07-03 17:47:35 +00:00
|
|
|
public function getAudioStream($url, $format, $password = null, $from = null, $to = null)
|
2016-07-29 22:15:17 +00:00
|
|
|
{
|
2016-10-20 21:13:37 +00:00
|
|
|
$video = $this->getJSON($url, $format, $password);
|
2018-05-23 19:50:06 +00:00
|
|
|
|
|
|
|
if (isset($video->_type) && $video->_type == 'playlist') {
|
|
|
|
throw new Exception(_('Conversion of playlists is not supported.'));
|
|
|
|
}
|
|
|
|
|
2018-07-03 17:47:35 +00:00
|
|
|
if (isset($video->protocol)) {
|
|
|
|
if (in_array($video->protocol, ['m3u8', 'm3u8_native'])) {
|
|
|
|
throw new Exception(_('Conversion of M3U8 files is not supported.'));
|
|
|
|
} elseif ($video->protocol == 'http_dash_segments') {
|
|
|
|
throw new Exception(_('Conversion of DASH segments is not supported.'));
|
|
|
|
}
|
2016-12-26 23:01:42 +00:00
|
|
|
}
|
2016-07-29 22:15:17 +00:00
|
|
|
|
2018-07-03 17:47:35 +00:00
|
|
|
$avconvProc = $this->getAvconvProcess($video, $this->config->audioBitrate, 'mp3', true, $from, $to);
|
2017-04-24 17:16:38 +00:00
|
|
|
|
2017-12-23 14:14:43 +00:00
|
|
|
$stream = popen($avconvProc->getCommandLine(), 'r');
|
2017-12-05 15:06:57 +00:00
|
|
|
|
|
|
|
if (!is_resource($stream)) {
|
2018-02-05 15:48:58 +00:00
|
|
|
throw new Exception(_('Could not open popen stream.'));
|
2017-12-05 15:06:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $stream;
|
2016-07-29 22:15:17 +00:00
|
|
|
}
|
2016-12-26 14:50:26 +00:00
|
|
|
|
2016-12-26 14:58:07 +00:00
|
|
|
/**
|
|
|
|
* Get video stream from an M3U playlist.
|
|
|
|
*
|
2018-02-05 15:48:58 +00:00
|
|
|
* @param stdClass $video Video object returned by getJSON
|
2016-12-26 14:58:07 +00:00
|
|
|
*
|
2018-02-05 15:48:58 +00:00
|
|
|
* @throws Exception If avconv/ffmpeg is missing
|
|
|
|
* @throws Exception If the popen stream was not created correctly
|
2017-12-19 14:20:52 +00:00
|
|
|
*
|
2017-12-05 15:06:57 +00:00
|
|
|
* @return resource popen stream
|
2016-12-26 14:58:07 +00:00
|
|
|
*/
|
2018-02-05 15:48:58 +00:00
|
|
|
public function getM3uStream(stdClass $video)
|
2016-12-26 14:50:26 +00:00
|
|
|
{
|
2017-05-13 22:52:59 +00:00
|
|
|
if (!$this->checkCommand([$this->config->avconv, '-version'])) {
|
2018-02-21 17:55:12 +00:00
|
|
|
throw new Exception(_('Can\'t find avconv or ffmpeg at ').$this->config->avconv.'.');
|
2016-12-26 14:50:26 +00:00
|
|
|
}
|
|
|
|
|
2017-12-23 13:17:29 +00:00
|
|
|
$process = new Process(
|
2016-12-26 14:50:26 +00:00
|
|
|
[
|
|
|
|
$this->config->avconv,
|
2017-12-23 14:14:43 +00:00
|
|
|
'-v', $this->config->avconvVerbosity,
|
2016-12-26 14:50:26 +00:00
|
|
|
'-i', $video->url,
|
|
|
|
'-f', $video->ext,
|
|
|
|
'-c', 'copy',
|
|
|
|
'-bsf:a', 'aac_adtstoasc',
|
|
|
|
'-movflags', 'frag_keyframe+empty_moov',
|
|
|
|
'pipe:1',
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2017-12-23 13:17:29 +00:00
|
|
|
$stream = popen($process->getCommandLine(), 'r');
|
2017-12-05 15:06:57 +00:00
|
|
|
if (!is_resource($stream)) {
|
2018-02-05 15:48:58 +00:00
|
|
|
throw new Exception(_('Could not open popen stream.'));
|
2017-12-05 15:06:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $stream;
|
2016-12-26 14:50:26 +00:00
|
|
|
}
|
2017-04-24 16:31:14 +00:00
|
|
|
|
2017-04-24 22:40:24 +00:00
|
|
|
/**
|
|
|
|
* Get an avconv stream to remux audio and video.
|
|
|
|
*
|
|
|
|
* @param array $urls URLs of the video ($urls[0]) and audio ($urls[1]) files
|
|
|
|
*
|
2018-02-05 15:48:58 +00:00
|
|
|
* @throws Exception If the popen stream was not created correctly
|
2017-12-19 14:20:52 +00:00
|
|
|
*
|
2017-12-05 15:06:57 +00:00
|
|
|
* @return resource popen stream
|
2017-04-24 22:40:24 +00:00
|
|
|
*/
|
|
|
|
public function getRemuxStream(array $urls)
|
|
|
|
{
|
2017-12-23 13:17:29 +00:00
|
|
|
$process = new Process(
|
2017-04-24 22:40:24 +00:00
|
|
|
[
|
|
|
|
$this->config->avconv,
|
2017-12-23 14:14:43 +00:00
|
|
|
'-v', $this->config->avconvVerbosity,
|
2017-04-24 22:40:24 +00:00
|
|
|
'-i', $urls[0],
|
|
|
|
'-i', $urls[1],
|
|
|
|
'-c', 'copy',
|
|
|
|
'-map', '0:v:0 ',
|
|
|
|
'-map', '1:a:0',
|
|
|
|
'-f', 'matroska',
|
|
|
|
'pipe:1',
|
|
|
|
]
|
|
|
|
);
|
2017-04-24 22:41:49 +00:00
|
|
|
|
2017-12-23 13:17:29 +00:00
|
|
|
$stream = popen($process->getCommandLine(), 'r');
|
2017-12-05 15:06:57 +00:00
|
|
|
if (!is_resource($stream)) {
|
2018-02-05 15:48:58 +00:00
|
|
|
throw new Exception(_('Could not open popen stream.'));
|
2017-12-05 15:06:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $stream;
|
2017-04-24 22:40:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-24 16:31:14 +00:00
|
|
|
/**
|
|
|
|
* Get video stream from an RTMP video.
|
|
|
|
*
|
2018-02-05 15:48:58 +00:00
|
|
|
* @param stdClass $video Video object returned by getJSON
|
2017-04-24 16:31:14 +00:00
|
|
|
*
|
2018-02-05 15:48:58 +00:00
|
|
|
* @throws Exception If the popen stream was not created correctly
|
2017-12-19 14:20:52 +00:00
|
|
|
*
|
2017-12-05 15:06:57 +00:00
|
|
|
* @return resource popen stream
|
2017-04-24 16:31:14 +00:00
|
|
|
*/
|
2018-02-05 15:48:58 +00:00
|
|
|
public function getRtmpStream(stdClass $video)
|
2017-04-24 16:31:14 +00:00
|
|
|
{
|
2017-12-23 14:14:43 +00:00
|
|
|
$process = new Process(
|
|
|
|
array_merge(
|
|
|
|
[
|
|
|
|
$this->config->avconv,
|
|
|
|
'-v', $this->config->avconvVerbosity,
|
|
|
|
],
|
|
|
|
$this->getRtmpArguments($video),
|
|
|
|
[
|
|
|
|
'-i', $video->url,
|
|
|
|
'-f', $video->ext,
|
|
|
|
'pipe:1',
|
|
|
|
]
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$stream = popen($process->getCommandLine(), 'r');
|
2017-12-05 15:06:57 +00:00
|
|
|
if (!is_resource($stream)) {
|
2018-02-05 15:48:58 +00:00
|
|
|
throw new Exception(_('Could not open popen stream.'));
|
2017-12-05 15:06:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $stream;
|
2017-04-24 16:31:14 +00:00
|
|
|
}
|
2017-05-02 15:04:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a Tar stream containing every video in the playlist piped through the server.
|
|
|
|
*
|
2017-05-04 22:06:18 +00:00
|
|
|
* @param object $video Video object returned by youtube-dl
|
2017-05-02 15:04:55 +00:00
|
|
|
* @param string $format Requested format
|
|
|
|
*
|
2018-02-05 15:48:58 +00:00
|
|
|
* @throws Exception If the popen stream was not created correctly
|
2017-12-19 14:20:52 +00:00
|
|
|
*
|
2017-12-05 15:06:57 +00:00
|
|
|
* @return resource
|
2017-05-02 15:04:55 +00:00
|
|
|
*/
|
2018-02-05 15:48:58 +00:00
|
|
|
public function getPlaylistArchiveStream(stdClass $video, $format)
|
2017-05-02 15:04:55 +00:00
|
|
|
{
|
|
|
|
$playlistItems = [];
|
|
|
|
foreach ($video->entries as $entry) {
|
|
|
|
$playlistItems[] = urlencode($entry->url);
|
|
|
|
}
|
|
|
|
$stream = fopen('playlist://'.implode(';', $playlistItems).'/'.$format, 'r');
|
2017-12-05 15:06:57 +00:00
|
|
|
if (!is_resource($stream)) {
|
2018-02-05 15:48:58 +00:00
|
|
|
throw new Exception(_('Could not open fopen stream.'));
|
2017-12-05 15:06:57 +00:00
|
|
|
}
|
2017-05-02 15:04:55 +00:00
|
|
|
|
|
|
|
return $stream;
|
|
|
|
}
|
2018-01-24 22:30:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the stream of a converted video.
|
|
|
|
*
|
|
|
|
* @param string $url URL of page
|
|
|
|
* @param string $format Source format to use for the conversion
|
|
|
|
* @param int $audioBitrate Audio bitrate of the converted file
|
|
|
|
* @param string $filetype Filetype of the converted file
|
|
|
|
* @param string $password Video password
|
|
|
|
*
|
2018-02-05 15:48:58 +00:00
|
|
|
* @throws Exception If your try to convert and M3U8 video
|
|
|
|
* @throws Exception If the popen stream was not created correctly
|
2018-01-24 22:30:24 +00:00
|
|
|
*
|
|
|
|
* @return resource popen stream
|
|
|
|
*/
|
|
|
|
public function getConvertedStream($url, $format, $audioBitrate, $filetype, $password = null)
|
|
|
|
{
|
|
|
|
$video = $this->getJSON($url, $format, $password);
|
|
|
|
if (in_array($video->protocol, ['m3u8', 'm3u8_native'])) {
|
2018-02-06 18:11:57 +00:00
|
|
|
throw new Exception(_('Conversion of M3U8 files is not supported.'));
|
2018-01-24 22:30:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$avconvProc = $this->getAvconvProcess($video, $audioBitrate, $filetype, false);
|
|
|
|
|
|
|
|
$stream = popen($avconvProc->getCommandLine(), 'r');
|
|
|
|
|
|
|
|
if (!is_resource($stream)) {
|
2018-02-05 15:48:58 +00:00
|
|
|
throw new Exception(_('Could not open popen stream.'));
|
2018-01-24 22:30:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $stream;
|
|
|
|
}
|
2014-03-13 19:07:56 +00:00
|
|
|
}
|