Skip to content

Instantly share code, notes, and snippets.

@donaldsteele
Last active December 17, 2019 20:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donaldsteele/22c4f49650704b5e3946821c887e4b40 to your computer and use it in GitHub Desktop.
Save donaldsteele/22c4f49650704b5e3946821c887e4b40 to your computer and use it in GitHub Desktop.
can a directory and encode the media using scene 2016 standards
<?php
/**
* Created by PhpStorm.
* User: don
* Date: 2/22/2019
* Time: 10:11 AM
* Scan a directory and encode the media using scene 2016 standards
* this will ouput a bash script (could be modified at line 27 to exec instead of echo to execute
*
* Usage::
* php encode.php > mymyscript.sh
* chmod 700 myscript.sh
* nohup ./myscript.sh &
*
*/
$extension = '.mp4';
$subdir = 'encoded';
$videoFilePath = '/mnt/vault/Media/';
$dm = new directorySearcher($videoFilePath, $extension);
foreach ($dm->files as $file) {
$fm = new fileManager($file,$extension,$subdir);
$ff = new encoder($fm);
echo "ffmpeg command => " . $ff->command . "\n";
}
class encoder {
public $command;
public function __construct($inputFM)
{
$template = 'ffmpeg -i "%s" -filter:v scale=-1:-1 -c:v libx264 -crf 19 -preset slow -level 4.1 -sws_flags lanczos -tune animation -c:a aac -vbr 5 -map 0 -c:s copy -strict -2 "%s"';
$this->command = sprintf($template,$inputFM->origFileFull, $inputFM->newFileFull);
}
}
class fileManager
{
public $origFileFull;
public $newFileFull;
public $outputPath;
public $inputPath;
public function __construct($fullFilePath, $extension, $outputSubDirectory)
{
$file = new SplFileInfo($fullFilePath);
$orig_file = $file->getBasename($extension);
$this->inputPath = $file->getPath();
$this->origFileFull = $this->inputPath . DIRECTORY_SEPARATOR . $orig_file . $extension;
$this->outputPath = $this->inputPath . DIRECTORY_SEPARATOR . $outputSubDirectory . DIRECTORY_SEPARATOR;
$this->newFileFull = $this->outputPath . $orig_file . $extension;
if (!file_exists($this->outputPath)) {
mkdir($this->outputPath, 0777, true);
}
}
}
class directorySearcher
{
public $files ;
function __construct($path, $filterExtension)
{
$real_path = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
$mask = '*{' . $filterExtension . '}';
$pattern = $real_path.$mask;
$this->files = glob($pattern, GLOB_BRACE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment