Last active
January 28, 2018 15:49
-
-
Save waqashassan98/54a4a11c3d83a5d1f11155b956435673 to your computer and use it in GitHub Desktop.
Cron Job to convert Videos to MP4 and WebM using ffmpeg library. It uses file locking to prevent duplicate cron jobs being running simultaneously
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 | |
$f = fopen('lock', 'w') or die ('Cannot create lock file'); | |
if (flock($f, LOCK_EX | LOCK_NB)) { | |
ini_set('max_execution_time', 0); | |
/** | |
Database Configurations | |
**/ | |
$servername = "localhost"; | |
$username = "xyz"; | |
$password = "password"; | |
$dbname = "db"; | |
$tbl_name = "profile_video"; // Table from which videos can be accessed | |
/** | |
Variables | |
**/ | |
$path_ffmpeg = '/usr/local/bin/ffmpeg'; | |
$path_ffprobe = '/usr/local/bin/ffprobe'; | |
$path_to_videos = '/path/to/web/videos/'; | |
// Create connection | |
$conn = new mysqli($servername, $username, $password, $dbname); | |
// Check connection | |
if ($conn->connect_error) { | |
die("Connection failed: " . $conn->connect_error); | |
} | |
echo "Connected successfully"; | |
$sql = "SELECT * FROM $tbl_name where converted = 0"; | |
$result = $conn->query($sql); | |
if ($result->num_rows > 0) { | |
// output data of each row | |
while($row = $result->fetch_assoc()) { | |
$videoname = $row["video_name"]; | |
$video_without_extension = substr($videoname, 0, strrpos($videoname, ".")); | |
$input_file_path = $path_to_videos.$videoname; | |
$output_mp4_name = $path_to_videos."converted-".$video_without_extension."-mp4.mp4"; | |
$output_webm_name = $path_to_videos."converted-".$video_without_extension."-webm.webm"; | |
$frame = $path_to_videos."converted-".$video_without_extension.".jpg"; | |
echo "Converting: ". $input_file_path; | |
exec($path_ffmpeg.' -i '.$input_file_path.' -s hd480 -vcodec libvpx -g 120 -lag-in-frames 16 -deadline good -cpu-used 0 -vprofile 0 -qmax 63 -qmin 0 -b:v 768k -acodec libvorbis -ab 112k -ar 44100 -f webm '.$output_webm_name); | |
exec($path_ffmpeg.' -y -i '. $input_file_path. ' -s hd480 -c:v libx264 -c:a copy '.$output_mp4_name); | |
//execute ffmpeg and create thumb | |
exec($path_ffmpeg.' -i '.$input_file_path.' -f mjpeg -vframes 1 -s 150x150 -an '.$frame.''); | |
//check if file exists (i.e it has been converted) | |
//then update in database | |
if (file_exists($output_webm_name) && file_exists($output_mp4_name)) { | |
$sqli = "update $tbl_name set converted = 1 where id = ".$row['id']; | |
if ($conn->query($sqli) === TRUE) { | |
echo "Record updated successfully"; | |
} else { | |
echo "Error updating record: " . $conn->error; | |
} | |
} | |
} | |
} else { | |
echo "0 results"; | |
} | |
$conn->close(); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment