Skip to content

Instantly share code, notes, and snippets.

@ibnux
Last active December 20, 2021 03:26
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save ibnux/d81a9ffaad1af618820f to your computer and use it in GitHub Desktop.
Save ibnux/d81a9ffaad1af618820f to your computer and use it in GitHub Desktop.
This script for converting all videos on your NAS, i use this script for converting video on my Synology NAS, This will recursively convert all your video
<?php
/*
Created by @ibnux January 2015
Use with your own risk
This script for converting all videos on your NAS
i use this script for converting video on my Synology NAS
This will recursively convert all your video
put this in your home folder
use command : nohup php -f video_convert_recursive.php > /dev/null &
this will start command in background, and you can close SSH
*/
//-------Configuration---------
//Change path to your Video Folder
$path = "/volume1/video/";
//result file
$GLOBALS['toExt'] = "mp4";
//list of supported video file to convert to $toExt
$GLOBALS['supportedFiles'] = ",avi,wmv,mkv,mp4,mov,flv,asf,mpg,mpeg,rm,rmvb,m4v,";
//this will appended to video ex: myvideo.mp4 -> myvideo.converted.mp4
$GLOBALS['fileName'] = "converted";
//if you want to remove original video
$GLOBALS['removeOriginal'] = true;
//minimum file size to convert
$GLOBALS['minSize'] = 250000000;
/*
Read This
https://ffmpeg.org/ffmpeg.html
http://edoceo.com/cli/ffmpeg
http://mattofalltrades42.hubpages.com/hub/FFmpeg-Command-Line-Examples-Resizing-Encoding-Transcoding-and-Comparing-Videos
http://www.catswhocode.com/blog/19-ffmpeg-commands-for-all-needs
my configuration below is to rescale video to 320p and use bitrate 512k, video for smartphone.
*/
$GLOBALS['ffmpeg'] = "-maxrate 512k -b:v 512k -vf scale=-1:320";
//get Extension File
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
function scanFolder($PathFolder){
$temp = opendir($PathFolder);
while ($folder = readdir($temp)) {
$path = $PathFolder.$folder;
//check is folder or not, minimum filesize and is already converted
$size = filesize($path);
if (!is_dir($path) &&
$size>$GLOBALS['minSize'] &&
strpos($path,".".$GLOBALS['fileName'].".")===false) {
$ext = getExtension($path);
if(strpos($GLOBALS['supportedFiles'],strtolower($ext))===false){
echo "NOT selected: ".$GLOBALS['supportedFiles']." $ext $path $size\r\n";
}else
convertVideos($path);
}else if (!in_array($folder, array("..", ".")) && is_dir($path)) {
//if found folder, scan again to sub folder
scanFolder($path."/");
}
}
closedir($temp);
}
function convertVideos($path){
$ext = getExtension($path);
//create target filename
//dirname to get Folder path without filename
//basename to get filename without folder path
$target = dirname($path)."/".basename($path,$ext).$GLOBALS['fileName'].".".$GLOBALS['toExt'];
$result = exec('ffmpeg -i "'.$path.'" '.$GLOBALS['ffmpeg'].' "'.$target.'"');
echo $result."\n";
if($GLOBALS['removeOriginal']){
//if file size converted file more than 1MB, remove original
if(file_exists($target) && filesize($target)>1000000){
unlink($path);
}
}
}
scanFolder($path);
@ibnux
Copy link
Author

ibnux commented Jan 26, 2015

Can anyone create this script for Python?
i want script like this on my Raspberry Pi with OpenElec installed

@sudoerss
Copy link

Hello,
I encounter an error while executin your script, I have the following error message

Encoder (codec ac3) not found for output stream #0:1

could you tell me if I have something more to install on my SIN
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment