-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrefresh-video.php
executable file
·61 lines (52 loc) · 1.85 KB
/
refresh-video.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/php
<?php
define( 'CACHE_DIR', $_SERVER['HOME'] . '/.cache/thumbnails/large/' );
function durationToSeconds($duration) {
list($hours, $minutes, $seconds) = sscanf($duration, "%d:%d:%f");
return $hours * 3600 + $minutes * 60 + $seconds;
}
function secondsToDuration($seconds) {
$hours = floor($seconds / 3600);
$seconds %= 3600;
$minutes = floor($seconds / 60);
$seconds %= 60;
echo "\n\nTIME: $hours, $minutes, $seconds";
return sprintf("%02d:%02d:%05.2f", $hours, $minutes, $seconds);
}
function get_file_path( $file_path ) {
// In case of samba file, it replaces the path for the "local" one
return preg_replace('#smb://([^/]+)/#', '/run/user/1000/gvfs/smb-share:server=$1,share=', $file_path);
}
function update_thumb( $file_path ) {
// ffmpeg -i input.mp4 -ss 00:00:01.000 -vframes 1 output.png
$inputFile = get_file_path($file_path);
$command = "ffmpeg -i $inputFile 2>&1 | grep 'Duration'";
$output = shell_exec($command);
if (preg_match('/Duration: (\d{2}:\d{2}:\d{2}\.\d{2})/', $output, $matches)) {
$duration = $matches[1];
// echo "Duration: $duration";
$totalSeconds = durationToSeconds($duration);
$frameSeconds = intval($totalSeconds * 0.1);
// echo "\n\nframeSeconds = $frameSeconds\n\n";
$halfDuration = secondsToDuration($frameSeconds);
}
echo $output . "\n";
$hash = md5( $file_path );
$outputFile = CACHE_DIR . $hash . '.png' ;
$startTime = $halfDuration;
$frames = 1;
$command = "ffmpeg -i $inputFile -ss $startTime -vframes $frames -y $outputFile > /dev/null 2>&1";
// echo $command . "\n";
exec($command, $output, $returnVar);
if ($returnVar === 0) {
echo "Thumbnail created successfully: $outputFile";
} else {
echo "Error creating thumbnail.";
print_r($output);
}
}
$files = explode( "\n", $_SERVER[ 'NAUTILUS_SCRIPT_SELECTED_URIS' ] );
foreach ( $files as $file ) {
echo $file."\n";
update_thumb( $file );
}