hls-playlist

This commit is contained in:
shimon
2022-09-04 09:29:56 +03:00
parent b13c9f58cc
commit 115b032f3a
5 changed files with 33 additions and 16 deletions
+2 -1
View File
@@ -656,8 +656,9 @@ App::get('/v1/videos/:videoId/streams/:streamId')
if ($value['type'] === 'audio') {
$_audios[] = [
'type' => 'group_audio',
'name' => 'audio_' . $value['id'],
'name' => $value['language'],
'default' => ($key === 0) ? 'YES' : 'NO',
'language' => $value['language'],
'uri' => $baseUrl . '/renditions/' . $rendition->getId() . '/types/' . $value['id'],
];
} elseif ($value['type'] === 'video') {
+2 -2
View File
@@ -844,8 +844,8 @@ App::setResource('project', function ($dbForConsole, $request, $console) {
/** @var Utopia\Database\Database $dbForConsole */
/** @var Utopia\Database\Document $console */
//$projectId = 'dev';
$projectId = $request->getParam('project', $request->getHeader('x-appwrite-project', 'console'));
$projectId = 'dev';
//$projectId = $request->getParam('project', $request->getHeader('x-appwrite-project', 'console'));
if ($projectId === 'console') {
return $console;
+2 -2
View File
@@ -1,12 +1,12 @@
#EXTM3U
#EXT-X-VERSION:3
<?php foreach ($this->getParam('audios', []) as $audio): ?>
#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="group_audio",NAME="<?php echo $audio['name']; ?>",DEFAULT=<?php echo $audio['default']; ?>,URI="<?php echo $audio['uri']; ?>"<?php echo PHP_EOL?>
#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="group_audio"<?php if($audio['name'] !== null):?>,NAME="<?php echo $audio['name'];?>"<?php endif;?>,DEFAULT=<?php echo $audio['default']; ?><?php if($audio['language'] !== null):?>,LANGUAGE="<?php echo $audio['language'];?>"<?php endif;?><?php if($audio['uri'] !== null):?>,URI="<?php echo $audio['uri'];?>"<?php endif;?><?php echo PHP_EOL?>
<?php endforeach; ?>
<?php foreach ($this->getParam('subtitles', []) as $subtitle): ?>
#EXT-X-MEDIA:TYPE=SUBTITLES,GROUP-ID="subs",NAME="<?php echo $subtitle['name']; ?>",DEFAULT=<?php echo $subtitle['default']; ?>,AUTOSELECT=NO,FORCED=NO,LANGUAGE="<?php echo $subtitle['code']; ?>",URI="<?php echo $subtitle['uri']; ?>"<?php echo PHP_EOL?>
<?php endforeach; ?>
<?php foreach ($this->getParam('renditions', []) as $rendition): ?>
#EXT-X-STREAM-INF:BANDWIDTH="<?php echo $rendition['bandwidth']; ?>",RESOLUTION="<?php echo $rendition['resolution']; ?>",NAME="<?php echo $rendition['name']; ?>"<?php if($rendition['subs'] !== null):?>,SUBTITLES="<?php echo $rendition['subs'];?>"<?php endif;?><?php if($rendition['subs'] !== null):?>,AUDIO="<?php echo $rendition['audio'];?>"<?php endif;?> <?php echo PHP_EOL?>
#EXT-X-STREAM-INF:BANDWIDTH="<?php echo $rendition['bandwidth']; ?>",RESOLUTION="<?php echo $rendition['resolution']; ?>",NAME="<?php echo $rendition['name']; ?>"<?php if($rendition['subs'] !== null):?>,SUBTITLES="<?php echo $rendition['subs'];?>"<?php endif;?><?php if($rendition['audio'] !== null):?>,AUDIO="<?php echo $rendition['audio'];?>"<?php endif;?> <?php echo PHP_EOL?>
<?php echo $rendition['uri'] ?><?php echo PHP_EOL?>
<?php endforeach; ?>
+26 -11
View File
@@ -48,7 +48,7 @@ class TranscodingV1 extends Worker
private string $renditionName;
private array $audios = [];
private array $audioTracks = [];
private Database $database;
@@ -75,12 +75,12 @@ class TranscodingV1 extends Worker
$sourceVideo = Authorization::skip(fn() => $this->database->findOne('videos', [new Query('_uid', Query::TYPE_EQUAL, [$this->args['videoId']])]));
if (empty($sourceVideo)) {
throw new Exception('Video not found');
throw new Exception('Video not found', 404, Exception::VIDEO_NOT_FOUND);
}
$profile = Authorization::skip(fn() => $this->database->findOne('videos_profiles', [new Query('_uid', Query::TYPE_EQUAL, [$this->args['profileId']])]));
if (empty($profile)) {
throw new Exception('profile not found');
throw new Exception('Video profile not found', 404, Exception::VIDEO_PROFILE_NOT_FOUND);
}
$bucket = Authorization::skip(fn() => $this->database->getDocument('buckets', $sourceVideo['bucketId']));
@@ -126,7 +126,9 @@ class TranscodingV1 extends Worker
}
foreach ($ffprobe->streams($inPath)->audios()->getIterator() as $stream) {
$this->audios[] = $stream->get('tags')['language'];
if (!empty($stream->get('tags')['language'])) {
$this->audioTracks[] = $stream->get('tags')['language'];
}
}
$audioStreamCount = $ffprobe->streams($inPath)->audios()->count();
@@ -353,15 +355,14 @@ class TranscodingV1 extends Worker
$query->setAttribute('status', self::STATUS_READY);
Authorization::skip(fn() => $this->database->updateDocument($collection, $query->getId(), $query));
} catch (\Throwable $th) {
var_dump($th->getCode());
var_dump($th->getMessage());
$query->setAttribute('metadata', json_encode([
'code' => $th->getCode(),
'message' => substr($th->getMessage(), 0, 3600),
'message' => substr($th->getMessage(), 0, 255),
]));
$query->setAttribute('status', self::STATUS_ERROR);
Authorization::skip(fn() => $this->database->updateDocument($collection, $query->getId(), $query));
throw new Exception($th->getMessage(), 500, Exception::GENERAL_UNKNOWN);
}
}
@@ -412,7 +413,7 @@ class TranscodingV1 extends Worker
}
$hls->setFormat($format)
->setAudiolanguages($this->audios)
->setAudioTracks($this->audioTracks)
->setHlsTime($segmentSize)
->setHlsAllowCache(false)
->addRepresentation($representation)
@@ -468,18 +469,32 @@ class TranscodingV1 extends Worker
$handle = fopen($path, "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
$line = str_replace([",","\r","\n"], "", $line);
$line = str_replace(['"'], '', $line);
$attributes = explode(',', $line);
$language = null;
foreach ($attributes as $attribute) {
if (str_contains($attribute, "LANGUAGE")) {
$parts = explode('=', $attribute);
$language = $parts[1];
}
}
$end = strpos($line, 'm3u8');
if ($end !== false) {
$start = strpos($line, $this->args['videoId']);
if ($start !== false) {
$path = substr($line, $start, ($end - $start) + 4);
$parts = explode('_', $path);
$files[] = [
$tmp = [
'id' => $parts[1],
'type' => str_contains($line, "TYPE=AUDIO") ? 'audio' : 'video',
'path' => $path
];
];
if (!empty($language)) {
$tmp ['language'] = $language;
}
$files[] = $tmp;
}
}
}
+1
View File
@@ -180,6 +180,7 @@ class Exception extends \Exception
public const VIDEO_SUBTITLE_SEGMENT_NOT_FOUND = 'video_subtitle_segment_not_found';
private $type = '';
public function __construct(string $message, int $code = 0, string $type = Exception::GENERAL_UNKNOWN, \Throwable $previous = null)