feat: Add a new convertAdvanced option

It allows user to convert videos to several other audio/video formats

Fixes #148
This commit is contained in:
Pierre Rudloff 2018-01-24 23:30:24 +01:00
parent c5e3c618f2
commit 4972c8ab8e
11 changed files with 221 additions and 14 deletions

View file

@ -249,15 +249,18 @@ class VideoDownload
}
/**
* Get a process that runs avconv in order to convert a video to MP3.
* Get a process that runs avconv in order to convert a video.
*
* @param object $video Video object returned by youtube-dl
* @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
*
* @throws \Exception If avconv/ffmpeg is missing
*
* @return Process Process
*/
private function getAvconvMp3Process(\stdClass $video)
private function getAvconvProcess(\stdClass $video, $audioBitrate, $filetype = 'mp3', $audioOnly = true)
{
if (!$this->checkCommand([$this->config->avconv, '-version'])) {
throw(new \Exception('Can\'t find avconv or ffmpeg'));
@ -269,6 +272,12 @@ class VideoDownload
$rtmpArguments = [];
}
if ($audioOnly) {
$videoArguments = ['-vn'];
} else {
$videoArguments = [];
}
$arguments = array_merge(
[
$this->config->avconv,
@ -277,9 +286,11 @@ class VideoDownload
$rtmpArguments,
[
'-i', $video->url,
'-f', 'mp3',
'-b:a', $this->config->audioBitrate.'k',
'-vn',
'-f', $filetype,
'-b:a', $audioBitrate.'k',
],
$videoArguments,
[
'pipe:1',
]
);
@ -311,7 +322,7 @@ class VideoDownload
throw(new \Exception('Conversion of M3U8 files is not supported.'));
}
$avconvProc = $this->getAvconvMp3Process($video);
$avconvProc = $this->getAvconvProcess($video, $this->config->audioBitrate);
$stream = popen($avconvProc->getCommandLine(), 'r');
@ -448,4 +459,36 @@ class VideoDownload
return $stream;
}
/**
* 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
*
* @throws \Exception If your try to convert and M3U8 video
* @throws \Exception If the popen stream was not created correctly
*
* @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'])) {
throw(new \Exception('Conversion of M3U8 files is not supported.'));
}
$avconvProc = $this->getAvconvProcess($video, $audioBitrate, $filetype, false);
$stream = popen($avconvProc->getCommandLine(), 'r');
if (!is_resource($stream)) {
throw new \Exception('Could not open popen stream.');
}
return $stream;
}
}