From 88cdcfa1e3f526ee0b7e4880679f79d46a59305a Mon Sep 17 00:00:00 2001 From: Kevin Ngo Date: Tue, 29 Jan 2019 15:54:47 -0800 Subject: [PATCH 1/4] provision ffmpeg for preview conversions --- Vagrantfile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Vagrantfile b/Vagrantfile index f6b652f..49b5c2a 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -46,4 +46,6 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| if defined? VagrantPlugins::HostsUpdater config.hostsupdater.aliases = settings['sites'].map { |site| site['map'] } end + + config.vm.provision 'ffmpeg', type: 'shell', inline: 'apt install -y ffmpeg' end From b842002ca5ec198410e40e38d8b585f36974457c Mon Sep 17 00:00:00 2001 From: Kevin Ngo Date: Tue, 29 Jan 2019 17:15:14 -0800 Subject: [PATCH 2/4] store mp3 previews for reliable cross-broser preview files --- app/SongComposer.php | 9 ++++++++- app/UploadParser.php | 8 +++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/app/SongComposer.php b/app/SongComposer.php index f89be72..7869e72 100644 --- a/app/SongComposer.php +++ b/app/SongComposer.php @@ -152,9 +152,16 @@ protected function createOrUpdate(array $metadata, string $file): array Storage::disk()->makeDirectory('public/songs/' . $song->id); } + // Store song image. File::move($file, storage_path('app/public/songs') . "/{$song->id}/{$song->id}-{$songDetails->id}.zip"); Storage::disk()->put("public/songs/{$song->id}/{$song->id}-{$songDetails->id}.{$songData['coverType']}", base64_decode($songData['coverData'])); + // Convert for preview. + Storage::disk()->put("public/songs/{$song->id}/tempPreview", base64_decode($songData['songPreviewData'])); + $previewTempPath = storage_path("app/public/songs/{$song->id}/tempPreview"); + $previewOutPath = storage_path("app/public/songs/{$song->id}/preview.mp3"); + exec("(ffmpeg -i {$previewTempPath} {$previewOutPath}; rm ${previewTempPath}) > /dev/null &"); + return [ 'status' => static::SONG_CREATED, 'key' => $song->id . '-' . $songDetails->id, @@ -448,4 +455,4 @@ protected function updateCache(array $song) } } -} \ No newline at end of file +} diff --git a/app/UploadParser.php b/app/UploadParser.php index be9b5d4..f247062 100644 --- a/app/UploadParser.php +++ b/app/UploadParser.php @@ -138,9 +138,15 @@ function map($v) } $hashBase = ''; + $hasSongPreviewData = false; foreach ($info['difficultyLevels'] as $difficultyLevel) { if ($this->zipHasFile($difficultyLevel['audioPath']) && $this->zipHasFile($difficultyLevel['jsonPath'])) { + if (!$hasSongPreviewData) { + $songData['songPreviewData'] = base64_encode($this->readFromZip($difficultyLevel['audioPath'])); + $hasSongPreviewData = true; + } + $songData['difficultyLevels'][$difficultyLevel['difficulty']] = [ 'difficulty' => $difficultyLevel['difficulty'], 'rank' => $difficultyLevel['difficultyRank'], @@ -290,4 +296,4 @@ protected function closeZip() $this->zipFile = null; } } -} \ No newline at end of file +} From 97e11482fc91d0ab67b9b351dadad03237f44d27 Mon Sep 17 00:00:00 2001 From: Kevin Ngo Date: Mon, 4 Feb 2019 08:16:45 -0800 Subject: [PATCH 3/4] store previews with original filenames --- app/SongComposer.php | 19 ++++++++++++++----- app/UploadParser.php | 9 +++++---- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/app/SongComposer.php b/app/SongComposer.php index 7869e72..38cf48b 100644 --- a/app/SongComposer.php +++ b/app/SongComposer.php @@ -156,11 +156,20 @@ protected function createOrUpdate(array $metadata, string $file): array File::move($file, storage_path('app/public/songs') . "/{$song->id}/{$song->id}-{$songDetails->id}.zip"); Storage::disk()->put("public/songs/{$song->id}/{$song->id}-{$songDetails->id}.{$songData['coverType']}", base64_decode($songData['coverData'])); - // Convert for preview. - Storage::disk()->put("public/songs/{$song->id}/tempPreview", base64_decode($songData['songPreviewData'])); - $previewTempPath = storage_path("app/public/songs/{$song->id}/tempPreview"); - $previewOutPath = storage_path("app/public/songs/{$song->id}/preview.mp3"); - exec("(ffmpeg -i {$previewTempPath} {$previewOutPath}; rm ${previewTempPath}) > /dev/null &"); + // Create MP3 previews (e.g., for iOS). + foreach ($songData['songPreviews'] as $audioPath => $songPreviewData) { + // Save original song data temporarily to convert with ffmpeg. + $audioFilename = basename($audioPath); + $storagePathBase = "public/songs/{$song->id}"; + $storagePathPreview = "{$storagePathBase}/temp-{$audioFilename}"; + Storage::disk()->put("$storagePathPreview", base64_decode($songPreviewData)); + + // Convert to MP3 with ffmpeg. + $audioFilenameBase = pathinfo($audioFilename)['filename']; + $previewOutPath = storage_path("app/{$storagePathBase}/{$audioFilenameBase}.mp3"); + $previewTempPath = storage_path("app/{$storagePathPreview}"); + exec("(ffmpeg -i {$previewTempPath} {$previewOutPath}; rm {$previewTempPath}) > /dev/null &"); + } return [ 'status' => static::SONG_CREATED, diff --git a/app/UploadParser.php b/app/UploadParser.php index f247062..5a9f6f8 100644 --- a/app/UploadParser.php +++ b/app/UploadParser.php @@ -138,13 +138,14 @@ function map($v) } $hashBase = ''; - $hasSongPreviewData = false; + $songData['songPreviews'] = []; foreach ($info['difficultyLevels'] as $difficultyLevel) { if ($this->zipHasFile($difficultyLevel['audioPath']) && $this->zipHasFile($difficultyLevel['jsonPath'])) { - if (!$hasSongPreviewData) { - $songData['songPreviewData'] = base64_encode($this->readFromZip($difficultyLevel['audioPath'])); - $hasSongPreviewData = true; + // Store song preview data in object for SongComposer to save to disk. + $audioPath = $difficultyLevel['audioPath']; + if (!array_key_exists($audioPath, $songData['songPreviews'])) { + $songData['songPreviews'][$audioPath] = base64_encode($this->readFromZip($audioPath)); } $songData['difficultyLevels'][$difficultyLevel['difficulty']] = [ From c667364d6cfd722047942a9cfd1dd6f152bfafa2 Mon Sep 17 00:00:00 2001 From: Jack Baron Date: Tue, 5 Feb 2019 22:35:34 +0000 Subject: [PATCH 4/4] Handle spaces in paths --- app/SongComposer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/SongComposer.php b/app/SongComposer.php index 38cf48b..11172f8 100644 --- a/app/SongComposer.php +++ b/app/SongComposer.php @@ -168,7 +168,7 @@ protected function createOrUpdate(array $metadata, string $file): array $audioFilenameBase = pathinfo($audioFilename)['filename']; $previewOutPath = storage_path("app/{$storagePathBase}/{$audioFilenameBase}.mp3"); $previewTempPath = storage_path("app/{$storagePathPreview}"); - exec("(ffmpeg -i {$previewTempPath} {$previewOutPath}; rm {$previewTempPath}) > /dev/null &"); + exec("(ffmpeg -i \"{$previewTempPath}\" \"{$previewOutPath}\"; rm \"{$previewTempPath}\") > /dev/null &"); } return [