From b13c9f58ccf5e73b8a15e07efac9d004e648e307 Mon Sep 17 00:00:00 2001 From: shimon Date: Wed, 31 Aug 2022 18:52:42 +0300 Subject: [PATCH] audio tracks --- app/config/collections.php | 8 +++--- app/workers/transcoding.php | 52 ++++++++++++++----------------------- 2 files changed, 24 insertions(+), 36 deletions(-) diff --git a/app/config/collections.php b/app/config/collections.php index 680f873507..917fa72ba7 100644 --- a/app/config/collections.php +++ b/app/config/collections.php @@ -3025,9 +3025,9 @@ $collections = [ ], [ '$id' => 'width', - 'type' => Database::VAR_STRING, + 'type' => Database::VAR_INTEGER, 'format' => '', - 'size' => Database::LENGTH_KEY, + 'size' => 0, 'signed' => false, 'required' => false, 'default' => null, @@ -3036,9 +3036,9 @@ $collections = [ ], [ '$id' => 'height', - 'type' => Database::VAR_STRING, + 'type' => Database::VAR_INTEGER, 'format' => '', - 'size' => Database::LENGTH_KEY, + 'size' => 0, 'signed' => false, 'required' => false, 'default' => null, diff --git a/app/workers/transcoding.php b/app/workers/transcoding.php index 22ef03a515..7b8f7d8304 100644 --- a/app/workers/transcoding.php +++ b/app/workers/transcoding.php @@ -14,7 +14,6 @@ use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Query; use Utopia\Database\Validator\Authorization; -use FFMpeg\FFProbe\DataMapping\StreamCollection; use Utopia\Storage\Compression\Algorithms\GZIP; use Captioning\Format\SubripFile; use Utopia\Storage\Device; @@ -49,7 +48,7 @@ class TranscodingV1 extends Worker private string $renditionName; - private string $audioStreamCount; + private array $audios = []; private Database $database; @@ -126,21 +125,29 @@ class TranscodingV1 extends Worker throw new Exception('Not an valid FFMpeg file "' . $inPath . '"'); } - $this->audioStreamCount = $ffprobe->streams($inPath)->audios()->count(); - $this->videoStreamCount = $ffprobe->streams($inPath)->videos()->count(); - - $general = $this->getVideoSourceInfo($ffprobe->streams($inPath)); - if (!empty($general)) { - foreach ($general as $key => $value) { - $sourceVideo->setAttribute($key, (string)$value); - } + foreach ($ffprobe->streams($inPath)->audios()->getIterator() as $stream) { + $this->audios[] = $stream->get('tags')['language']; + } + $audioStreamCount = $ffprobe->streams($inPath)->audios()->count(); + $videoStreamCount = $ffprobe->streams($inPath)->videos()->count(); + $streams = $ffprobe->streams($inPath); + $sourceVideo + ->setAttribute('duration', $videoStreamCount > 0 ? $streams->videos()->first()->get('duration') : null) + ->setAttribute('height', $videoStreamCount > 0 ? $streams->videos()->first()->get('height') : null) + ->setAttribute('width', $videoStreamCount > 0 ? $streams->videos()->first()->get('width') : null) + ->setAttribute('videoCodec', $videoStreamCount > 0 ? $streams->videos()->first()->get('codec_name') : null) + ->setAttribute('videoFramerate', $videoStreamCount > 0 ? $streams->videos()->first()->get('avg_frame_rate') : null) + ->setAttribute('videoBitrate', $videoStreamCount > 0 ? $streams->videos()->first()->get('bit_rate') : null) + ->setAttribute('audioCodec', $audioStreamCount > 0 ? $streams->audios()->first()->get('codec_name') : null) + ->setAttribute('audioSamplerate', $audioStreamCount > 0 ? $streams->audios()->first()->get('sample_rate') : null) + ->setAttribute('audioBitrate', $audioStreamCount > 0 ? $streams->audios()->first()->get('bit_rate') : null) + ; Authorization::skip(fn() => $this->database->updateDocument( 'videos', $sourceVideo->getId(), $sourceVideo )); - } $video = $ffmpeg->open($inPath); $this->setRenditionName($profile); @@ -254,7 +261,7 @@ class TranscodingV1 extends Worker Authorization::skip(function () use ($segment, $project, $query, $renditionPath, $ref) { return $this->database->createDocument('videos_renditions_segments', new Document([ 'renditionId' => $query->getId(), - 'representationId' => $ref['id']+0, + 'representationId' => $ref['id'] + 0, 'fileName' => $segment['fileName'], 'path' => $renditionPath, 'duration' => $segment['duration'], @@ -405,7 +412,7 @@ class TranscodingV1 extends Worker } $hls->setFormat($format) - ->setAudioStreamCount($this->audioStreamCount) + ->setAudiolanguages($this->audios) ->setHlsTime($segmentSize) ->setHlsAllowCache(false) ->addRepresentation($representation) @@ -518,25 +525,6 @@ class TranscodingV1 extends Worker ]; } - /** - * @param $streams StreamCollection - * @return array - */ - private function getVideoSourceInfo(StreamCollection $streams): array - { - return [ - 'duration' => $streams->videos()->count() > 0 ? $streams->videos()->first()->get('duration') : '0', - 'height' => $streams->videos()->count() > 0 ? $streams->videos()->first()->get('height') : '0', - 'width' => $streams->videos()->count() > 0 ? $streams->videos()->first()->get('width') : '0', - 'videoCodec' => $streams->videos()->count() > 0 ? $streams->videos()->first()->get('codec_name') : '', - 'videoFramerate' => $streams->videos()->count() > 0 ? $streams->videos()->first()->get('avg_frame_rate') : '0', - 'videoBitrate' => $streams->videos()->count() > 0 ? $streams->videos()->first()->get('bit_rate') : '0', - 'audioCodec' => $streams->audios()->count() > 0 ? $streams->audios()->first()->get('codec_name') : '', - 'audioSamplerate' => $streams->audios()->count() > 0 ? $streams->audios()->first()->get('sample_rate') : '0', - 'audioBitrate' => $streams->audios()->count() > 0 ? $streams->audios()->first()->get('bit_rate') : '0', - ]; - } - /** * @param $metadata array * @return array