-
-
Save Upinel/f22e7cff587081eaccf26b8dc9cba646 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
This file contains hidden or 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 | |
/* | |
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment