Skip to content

Instantly share code, notes, and snippets.

@janpecha
Created September 2, 2012 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save janpecha/3601822 to your computer and use it in GitHub Desktop.
Save janpecha/3601822 to your computer and use it in GitHub Desktop.
Converting media files to MP3. Vyžaduje `ffmpeg`.
#!/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" -- $*
<?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;
}
@janpecha
Copy link
Author

Moved to my scripts repository. Go to http://www.github.com/janpecha/scripts.

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