Converting media files to MP3. Vyžaduje `ffmpeg`.
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
#!/bin/bash | |
# saves the path to this script's directory | |
dir=` dirname $0 ` | |
# absolutizes the path if necessary | |
if echo $dir | grep -v ^/ > /dev/null; then | |
dir=` pwd `/$dir | |
fi | |
# runs webgen.phpc with script's arguments | |
php -f "$dir/convert2mp3.phpc" -- $* |
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 | |
/** | |
* @author Jan Pecha, <janpecha@email.cz> | |
* @version 2012-09-02-1 | |
* @license New BSD License | |
*/ | |
if(($dir = getcwd()) !== false) | |
{ | |
include __DIR__ . '/nette.min.php'; | |
if(isset($_SERVER['argc']) && $_SERVER['argc'] > 1) // php convert2mp3 mp4 flv | |
{ | |
$first = true; | |
$errors = array(); | |
$masks = array(); | |
foreach($_SERVER['argv'] as $arg) | |
{ | |
if($first) | |
{ | |
$first = false; | |
continue; | |
} | |
if(checkExt($arg)) | |
{ | |
$masks[] = "*.$arg"; | |
} | |
else | |
{ | |
$errors[] = $arg; | |
} | |
} | |
if(count($errors)) | |
{ | |
foreach($errors as $ext) | |
{ | |
echo "[error] Pripona '$ext' je divna, zkus to opravit nebo smazat (pripona muze obsahovat pouze a-Z a 0-9 a nesmi byt 'mp3')\n"; | |
} | |
exit; | |
} | |
$numOfFiles = 0; | |
$errors = array(); | |
foreach (\Nette\Utils\Finder::findFiles($masks)->in($dir) as $file) // neprohledava podadresare | |
{ | |
if(($pos = strrpos($file, '.')) !== false) | |
{ | |
$nFile = substr($file, 0, $pos) . '.mp3'; | |
#echo "$nFile\n"; | |
passthru("ffmpeg -i \"$file\" \"$nFile\""); | |
echo "-------------------------------------------\n"; | |
} | |
else | |
{ | |
$errors[] = $file; | |
} | |
#echo "$file\n"; | |
$numOfFiles++; | |
} | |
echo "Zpracovano $numOfFiles souboru\n"; | |
if(count($errors)) | |
{ | |
echo "Nasledujici soubory nebyly zpracovany:\n"; | |
foreach($errors as $file) | |
{ | |
echo "$file\n"; | |
} | |
} | |
#echo "$numOfFiles\n"; | |
} | |
else | |
{ | |
echo "[error] Nebyly predany pripony souboru, nebo neni program spousten z prikazove radky\n"; | |
} | |
} | |
else | |
{ | |
echo "[error] Nepodarilo se ziskat cestu k adresari\n"; | |
} | |
function checkExt($ext) | |
{ | |
$allowedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; | |
if($ext == 'mp3') | |
{ | |
return false; | |
} | |
$len = strlen($ext); | |
for($i = 0; $i < $len; $i++) | |
{ | |
if(strpos($allowedChars, $ext[$i]) === false) | |
{ | |
return false; | |
} | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Moved to my
scripts
repository. Go to http://www.github.com/janpecha/scripts.