Skip to content

A self-hosted media server and file management system

use exiftool when adding uid and fallback to ffmpeg on error

aminnausin44fe5dde824f
diff --git a/app/Jobs/EmbedUidInMetadata.php b/app/Jobs/EmbedUidInMetadata.php
index d0ec52fd..fea7fb8b 100644
--- a/app/Jobs/EmbedUidInMetadata.php
+++ b/app/Jobs/EmbedUidInMetadata.php
@@ -145,6 +145,50 @@ class EmbedUidInMetadata implements ShouldQueue {
$formatMap = ['mp4' => 'mp4', 'mkv' => 'matroska', 'mp3' => 'mp3', 'ogg' => 'opus', 'flac' => 'flac'];
$format = $formatMap[$ext] ?? $ext;
+ if ($ext !== 'mp3' && $ext !== 'flac') {
+ try {
+ $this->addMetadataWithExifTool();
+
+ return ' ExifTool';
+ } catch (\Exception $e) {
+ dump('ExifTool failed, falling back to ffmpeg: ' . $e->getMessage());
+ $this->addMetadataWithFFMpeg($format, $tempFilePath);
+ }
+ }
+
+ if (file_exists($tempFilePath)) {
+ dump("Cleaning up temporary file: $tempFilePath");
+ unlink($tempFilePath); // delete the temp file
+ }
+
+ return ' FFmpeg';
+ }
+
+ private function addMetadataWithExifTool() {
+ dump('Attempting to add uuid using exiftool');
+
+ $command = [
+ 'exiftool',
+ '-uuid=' . $this->uuid,
+ '-overwrite_original',
+ $this->filePath,
+ ];
+
+ // Execute the exiftool command
+ $process = new Process($command);
+ $process->setTimeout(3600);
+ $process->run();
+
+ if (! $process->isSuccessful()) {
+ throw new \Exception('ExifTool failed: ' . $process->getErrorOutput());
+ }
+
+ dump('ExifTool succeeded in adding uuid');
+ }
+
+ private function addMetadataWithFFMpeg($format, $tempFilePath) {
+ dump('Fallback to ffmpeg to add uuid');
+
$command = [
'ffmpeg',
'-i',
@@ -170,12 +214,10 @@ class EmbedUidInMetadata implements ShouldQueue {
if (file_exists($tempFilePath)) {
// Get the original file's timestamps
- // $originalCreatedTime = filectime($this->filePath);
$originalModifiedTime = filemtime($this->filePath);
// Replace the original file with the temporary file
rename($tempFilePath, $this->filePath);
// Restore the original timestamps
- // touch($this->filePath, $originalModifiedTime, $originalCreatedTime);
touch($this->filePath, $originalModifiedTime);
} else {
throw new \Exception('Failed to create the temporary file with metadata.');
@@ -196,16 +238,14 @@ class EmbedUidInMetadata implements ShouldQueue {
$filePath,
];
- // Execute the FFmpeg command
$process = new Process($command);
$process->run();
- // Check if the process was successful
if (! $process->isSuccessful()) {
throw new ProcessFailedException($process);
}
- $output = $process->getOutput(); // Decode JSON output
+ $output = $process->getOutput();
$metadata = json_decode($output, true);
if ($ext === 'ogg') {
$metadata['format'] = $metadata['streams'][0] ?? [];
diff --git a/app/Jobs/IndexFiles.php b/app/Jobs/IndexFiles.php
index 32686a10..a985d4b3 100755
--- a/app/Jobs/IndexFiles.php
+++ b/app/Jobs/IndexFiles.php
@@ -90,6 +90,8 @@ class IndexFiles implements ShouldBeUnique, ShouldQueue {
'ended_at' => $endedAt,
'duration' => $duration,
]);
+ } catch (BatchCancelledException $e) {
+ $this->taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::CANCELLED, 'summary' => 'Parent Task was Cancelled During the Task']);
} catch (\Throwable $th) {
$endedAt = now();
$duration = (int) $this->startedAt->diffInSeconds($endedAt);
@@ -213,12 +215,16 @@ class IndexFiles implements ShouldBeUnique, ShouldQueue {
$duration = $metadataChange['duration'];
$date_scanned = $metadataChange['date_scanned'];
- $dbOut .= "INSERT INTO [metadata] VALUES ({$videoID}, {$compositeID}, {$uuid}, {$file_size}, {$duration}, {$date_scanned});\n\n"; // insert
+ $dbOut .= "UPSERT INTO [metadata] VALUES ({$videoID}, {$compositeID}, {$uuid}, {$file_size}, {$duration}, {$date_scanned});\n\n"; // upsert
array_push($metadataTransactions, $metadataChange);
}
try {
+ if ($this->batch()->cancelled()) {
+ throw new BatchCancelledException;
+ }
+
Video::destroy($videoDeletions);
Folder::destroy($folderDeletions);
Category::destroy($categoryDeletions);
@@ -254,6 +260,8 @@ class IndexFiles implements ShouldBeUnique, ShouldQueue {
dump('Categories | Folders | Videos | Changes | SQL ', $directories, ['count' => count($subDirectories['data']['folderStructure'])], ['count' => count($files['data']['videoStructure'])], $data, $dbOut);
return 'Changed ' . count($data['categories']) . ' libraries, ' . count($data['folders']) . ' folders and ' . count($data['videos']) . " Videos. \n\n$dbOut";
+ } catch (BatchCancelledException $e) {
+ throw $e;
} catch (\Throwable $th) {
dump($th);
throw new \Exception('Unable to index files, ' . $th->getMessage());
@@ -286,6 +294,10 @@ class IndexFiles implements ShouldBeUnique, ShouldQueue {
continue;
} // ? . and .. are dirs
+ if ($this->batch()->cancelled()) {
+ throw new BatchCancelledException;
+ }
+
$name = basename($local);
if (isset($stored[$name])) { // If scanned is in stored, add to current, remove from stored
@@ -354,6 +366,9 @@ class IndexFiles implements ShouldBeUnique, ShouldQueue {
$folders = Storage::disk('public')->directories("$path/$category"); // Immediate folders (dont scan sub folders)
foreach ($folders as $folder) {
+ if ($this->batch()->cancelled()) {
+ throw new BatchCancelledException;
+ }
$cost++;
$name = basename($folder);
@@ -446,6 +461,9 @@ class IndexFiles implements ShouldBeUnique, ShouldQueue {
dump("$path$folder");
foreach ($files as $file) {
+ if ($this->batch()->cancelled()) {
+ throw new BatchCancelledException;
+ }
$cost++;
$absolutePath = str_replace('\\', '/', Storage::disk('public')->path('')) . $file;
@@ -582,3 +600,4 @@ class IndexFiles implements ShouldBeUnique, ShouldQueue {
}
}
}
+class BatchCancelledException extends \Exception {}