Last active
December 18, 2015 00:39
-
-
Save vierbergenlars/5698258 to your computer and use it in GitHub Desktop.
Convert video files to mp3 audio.
Processes some videos simultaneous for better use of multi-core systems.
Requires avconv.
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
| #!/usr/bin/env php | |
| <?php | |
| // ##### START config | |
| $files = glob('*.{mp4,flv}', GLOB_BRACE); // File types to convert to mp3 (video only) | |
| $worker_num = 3; // number of cores +1 | |
| // ##### END config | |
| $len = count($files); | |
| $worker_pids = array(); | |
| function pr($s) { | |
| echo '['.getmypid().'] '.$s.PHP_EOL; | |
| } | |
| for($i=0; $i < $worker_num; $i++) { | |
| $re=pcntl_fork(); | |
| switch($re) { | |
| case -1: | |
| pr("Worker wouldn't fork. Kill all"); | |
| foreach($worker_pids as $pid) { | |
| posix_kill($pid, 9); | |
| } | |
| exit(1); | |
| break; | |
| case 0: | |
| pr("Worker $i online"); | |
| $start = $i*ceil($len/$worker_num); | |
| $end = ($i+1)*ceil($len/$worker_num)-1; | |
| pr("Allocated $start - $end"); | |
| $child = true; | |
| break; | |
| default: | |
| $worker_pids[] = $re; | |
| $child = false; | |
| } | |
| if($child) break; | |
| } | |
| if($child) { | |
| pr("Starting conversion"); | |
| for($i = $start; $i =< $end; $i ++) { | |
| $file = $files[$i]; | |
| if(file_exists($file.'.mp3')) { | |
| pr("Skipping $i (already exists)"); | |
| continue; | |
| } | |
| system('avconv -i "'.$file.'" -vn -y -f mp3 "'.$file.'.mp3" 2>/dev/null'); | |
| pr(($i-$start)."/".($end-$start)." [$i]"); | |
| } | |
| pr("Done."); | |
| } | |
| else { | |
| pr("Waiting for workers..."); | |
| foreach($worker_pids as $pid) { | |
| pcntl_waitpid($pid, $status); | |
| if(!pcntl_wifexited($status)) { | |
| pr("Abnormal death of $pid"); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment