Experimental support for M3U streams

This commit is contained in:
Pierre Rudloff 2016-12-26 15:50:26 +01:00
parent 1a6e995262
commit 28a8addbf1
5 changed files with 56 additions and 14 deletions

View file

@ -38,7 +38,12 @@ class Config
*
* @var array
*/
public $params = ['--no-playlist', '--no-warnings', '-f best[protocol^=http]', '--playlist-end', 1];
public $params = [
'--no-playlist', '--no-warnings',
//We can allow non-HTTP URLs on the feature/stream branch
'-f best',
'--playlist-end', 1
];
/**
* Enable audio conversion.

View file

@ -292,4 +292,28 @@ class VideoDownload
return popen($chain->getProcess()->getCommandLine(), 'r');
}
public function getM3uStream(\stdClass $video)
{
if (!shell_exec('which '.$this->config->avconv)) {
throw(new \Exception('Can\'t find avconv or ffmpeg'));
}
$procBuilder = ProcessBuilder::create(
[
$this->config->avconv,
'-v', 'quiet',
'-i', $video->url,
'-f', $video->ext,
'-c', 'copy',
'-bsf:a', 'aac_adtstoasc',
'-movflags', 'frag_keyframe+empty_moov',
'pipe:1',
]
);
//dump($procBuilder->getProcess()->getCommandLine()); die;
return popen($procBuilder->getProcess()->getCommandLine(), 'r');
}
}

View file

@ -153,7 +153,7 @@ class FrontController
}
if (isset($params['audio'])) {
try {
return $this->getStream($params['url'], 'mp3[protocol^=http]', $response, $request, $password);
return $this->getStream($params['url'], 'mp3', $response, $request, $password);
} catch (PasswordException $e) {
return $this->password($request, $response);
} catch (\Exception $e) {
@ -223,14 +223,22 @@ class FrontController
$format = 'best';
}
$video = $this->download->getJSON($url, $format, $password);
$client = new \GuzzleHttp\Client();
$stream = $client->request('GET', $video->url, ['stream' => true]);
$response = $response->withHeader('Content-Disposition', 'attachment; filename="'.$video->_filename.'"');
$response = $response->withHeader('Content-Type', $stream->getHeader('Content-Type'));
$response = $response->withHeader('Content-Length', $stream->getHeader('Content-Length'));
if ($request->isGet()) {
$response = $response->withBody($stream->getBody());
if ($video->protocol == 'm3u8') {
$stream = $this->download->getM3uStream($video);
$response = $response->withHeader('Content-Type', 'video/'.$video->ext);
if ($request->isGet()) {
$response = $response->withBody(new Stream($stream));
}
} else {
$client = new \GuzzleHttp\Client();
$stream = $client->request('GET', $video->url, ['stream' => true]);
$response = $response->withHeader('Content-Type', $stream->getHeader('Content-Type'));
$response = $response->withHeader('Content-Length', $stream->getHeader('Content-Length'));
if ($request->isGet()) {
$response = $response->withBody($stream->getBody());
}
}
$response = $response->withHeader('Content-Disposition', 'attachment; filename="'.$video->_filename.'"');
return $response;
}

View file

@ -26,18 +26,17 @@
<input type="hidden" name="url" value="{$video->webpage_url}" />
<select name="format" id="format" class="formats monospace">
<optgroup label="Generic formats">
<option value="best[protocol^=http]">
<option value="best">
{strip}
Best ({$video->ext})
{/strip}
</option>
<option value="worst[protocol^=http]">
<option value="worst">
Worst
</option>
</optgroup>
<optgroup label="Detailed formats" class="monospace">
{foreach $video->formats as $format}
{if $format->protocol|in_array:array('http', 'https')}
{strip}
<option value="{$format->format_id}">
{$format->ext}
@ -70,14 +69,13 @@
&nbsp;({$format->format_id})
</option>
{/strip}
{/if}
{/foreach}
</optgroup>
</select><br/><br/>
<input class="downloadBtn" type="submit" value="Download" /><br/>
</form>
{else}
<input type="hidden" name="format" value="best[protocol^=http]" />
<input type="hidden" name="format" value="best" />
<a class="downloadBtn"
href="{$video->url|escape}">Download</a><br/>
{/if}

View file

@ -176,6 +176,13 @@ class VideoDownloadTest extends \PHPUnit_Framework_TestCase
'edgefcs.net',
'GRIP sucht den Sommerkönig-folge-203-0.mp3',
],
[
//Only works on the feature/stream branch
'https://twitter.com/verge/status/813055465324056576/video/1', null,
'The Verge - This tiny origami robot can self-fold and complete tasks-813055465324056576.mp4',
'video.twimg.com',
'The Verge - This tiny origami robot can self-fold and complete tasks-813055465324056576.mp3',
]
];
}