Created
June 25, 2019 13:20
-
-
Save MarceauKa/9ab87bf07b612cf44304facbc19eaf61 to your computer and use it in GitHub Desktop.
Movies 2 sheet - Transform a folder with movies in a sheet with meta infos (files must be : "name YEAR.extension")
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$config = [ | |
'path' => './', | |
'ffmpeg' => '/usr/local/bin/ffmpeg', | |
'output' => 'movies2sheet.csv', | |
'col_separator' => ',', | |
'line_separator' => "\n", | |
]; | |
function getFiles() { | |
global $config; | |
$filesInDir = glob($config['path'] . '*'); | |
$files = []; | |
foreach ($filesInDir as $file) { | |
$files[] = parseFilename($file); | |
} | |
return $files; | |
} | |
function parseFilename($file) { | |
$matches = []; | |
preg_match('/^(.*)\s([0-9]{4})\.([a-z0-9]{2,4})$/u', basename($file), $matches); | |
return [ | |
'path' => $file, | |
'filename' => basename($file), | |
'name' => trim($matches[1]), | |
'year' => $matches[2], | |
'extension' => $matches[3], | |
]; | |
} | |
function getFileinfo($file) { | |
global $config; | |
// video infos | |
$command = $config['ffmpeg'] . ' -i ' . escapeshellarg($file['path']) . ' -vstats 2>&1'; | |
$output = shell_exec($command); | |
$regex_sizes = "/Video: ([^\r\n]*), ([^,]*), ([0-9]{1,4})x([0-9]{1,4})/iu"; | |
if (preg_match($regex_sizes, $output, $regs)) { | |
$codec = $regs[1] ?? null; | |
$width = $regs[3] ?? null; | |
$height = $regs[4] ?? null; | |
} | |
$regex_duration = '/Duration: ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2}).([0-9]{1,2})/iu'; | |
if (preg_match($regex_duration, $output, $regs)) { | |
$hours = $regs[1] ?? null; | |
$mins = $regs[2] ?? null; | |
} | |
$file['size'] = getHumanSize(filesize($file['path'])); | |
$file['codec'] = $codec ?? '-'; | |
$file['resolution'] = $width && $height ? sprintf('%dx%d', $width, $height) : '-'; | |
$file['duration'] = $hours && $mins ? sprintf('%dh%d', $hours, $mins) : '-'; | |
return $file; | |
} | |
function getHumanSize($bytes, $decimals = 2) { | |
$sz = 'BKMGTP'; | |
$factor = floor((strlen($bytes) - 1) / 3); | |
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$sz[$factor]; | |
} | |
function getFilerow($file) { | |
global $config; | |
return implode($config['col_separator'], [ | |
sprintf('"%s"', $file['name']), | |
$file['year'], | |
$file['duration'], | |
$file['resolution'], | |
$file['size'], | |
$file['extension'], | |
$file['codec'], | |
]); | |
} | |
function getHeaderRow() { | |
global $config; | |
return implode($config['col_separator'], [ | |
'Nom', | |
'Année', | |
'Durée', | |
'Résolution', | |
'Taille', | |
'Extension', | |
'Codec', | |
]); | |
} | |
function generateCsv() { | |
global $config; | |
$files = getFiles(); | |
$lines = [getHeaderRow()]; | |
foreach ($files as $file) { | |
$lines[] = getFilerow(getFileinfo($file)); | |
} | |
$output = implode($config['line_separator'], $lines); | |
echo vsprintf('File %s generated with %d movies', [ | |
basename($config['output']), | |
count($lines) - 1, | |
]); | |
file_put_contents($config['output'], $output); | |
} | |
generateCsv(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment