mplayer im Hintergrund

Status
Für weitere Antworten geschlossen.

warry

Benutzer
Mitglied seit
09. Okt 2010
Beiträge
123
Punkte für Reaktionen
0
Punkte
16
Hallo,

zur Beschallung meines Ladenlokals hab ich mir gedacht, ein script im Hintergrund laufen zu lassen, das einen Radiosender streamed und alle 20 Minuten einen aktuellen Spot dazwischenschiebt. Jetzt hab ich mal mit php etwas gebastelt, das zumindest funktioniert. Vielleicht habt ihr ja noch Ideen oder Anregungen dazu, vielleicht auch ganz andere Lösungsansätze...

Ich starte das script aktuelle per nohup von der Konsole und es scheint alles zu klappen. Würde mir dann noch entsprechendes Webinterface dazu basteln.

Meine Programmierkenntnisse sind eher als rudimentär einzustufen...

Rich (BBCode):
<?
$oldpath = "a";
$scr = "Ha";

while ($scr !== "Ho") {

 $woche = date("W",time());$jahr = date("Y",time());
 $path = "/volume1/music/spots/$jahr$woche*.mp3";
 if ($path !== $oldpath) {
	$cc = 0;
	$play = 0;
	$oldpath = $path;
	 if (is_array(glob($path))) {
		foreach (glob($path) as $filename) {
	    	 $cc++;
		 $play = 1;
		 $spots[$cc] = $filename;
		 $len[$cc] = exec("ffmpeg -i $filename 2>&1 |grep Duration |awk '{print $2}' |awk -F: '{print ($1 * 3600) + ($2 * 60 ) + ($3)}'");
		 $len[$cc] = round($len[$cc] + 0.5);
		 }
	       }
 print_r($spots); print_r($len);
 }
 

 if ($cc > 0) {
	$test = exec ("killall mplayer; /var/packages/AudioStation/target/bin/mplayer $spots[$play] -loop 0 -daemon -volume 50"); 
	echo "$test\n";
	sleep($len[$play]);
	$play++;
	if ($play > $cc) {$play = 1;}
	}

 $test = exec ("killall mplayer; /var/packages/AudioStation/target/bin/mplayer http://edge.live.mp3.mdn.newmedia.nacamar.net/ps-antennenr/livestream.mp3.m3u -loop 0 -daemon -volume 50"); 
 echo "$test\n";
 sleep(1200);
}
?>
 

warry

Benutzer
Mitglied seit
09. Okt 2010
Beiträge
123
Punkte für Reaktionen
0
Punkte
16
So, damit kann ich jetzt obiges script steuern.

Rich (BBCode):
<body bgcolor="#006666"> 
<form action="radio.php"> 
   <div align="center"> <input name="On"  style="font-size: 20pt; height: 40px; width: 600px" type="submit" value="On"/></div> 
   <div align="center"> <input name="Off" style="font-size: 20pt; height: 40px; width: 600px" type="submit" value="Off"/></div> 
   <div align="center"> <input name="Status" style="font-size: 20pt; height: 40px; width: 600px" type="submit" value="Status"/></div> 


<? 
$pidm = 0; $pidp = 0;
$pidm = exec("pidof mplayer"); 
$pidp = exec("pidof php radio2.php"); 
exec("ps w | grep mplayer 2>&1", $output); 



if (isset($_GET["On"]) && $_GET["On"] == "On") { 
   if ($pidm > 0) exec("killall mplayer"); 
   if ($pidp > 0) exec("kill $pidp"); 
   exec ("nohup php /usr/syno/synoman/webman/radio2.php >/usr/syno/synoman/webman/radio2.log &"); 
   sleep(1); 
   $pidm = 0; $pidp = 0;
   $pidm = exec("pidof mplayer"); 
   $pidp = exec("pidof php radio2.php"); 
   $output = "";
   exec("ps w | grep mplayer 2>&1", $output); 

} elseif (isset($_GET["Off"]) && $_GET["Off"] == "Off") { 
   if ($pidm > 0) exec("killall mplayer"); 
   if ($pidp > 0) exec("kill $pidp"); 
   sleep(3); 
   $pidm = 0; $pidp = 0;
   $pidm = exec("pidof mplayer"); 
   $pidp = exec("pidof php radio2.php"); 
   $output = "";
   exec("ps w | grep mplayer 2>&1", $output); 
} 
?> 

<div>  
<? echo "<br>mplayer: $pidm<br>radio2.php: $pidp<br>" ?>
</div> 
<div>
<? echo "<small><pre>";print_r($output); echo "</pre></small>"; ?>
</div>
<br> 
<br> 
<br> 
</form>
 

warry

Benutzer
Mitglied seit
09. Okt 2010
Beiträge
123
Punkte für Reaktionen
0
Punkte
16
Aktuelle Version, Lautstärke wird jetzt gespeichert.

Meinungen und Tipps gerne willkommen...

Kontroll-Script:
Rich (BBCode):
<body bgcolor="#006666"> 
<form action="index.php"> 
   <div align="center"> <input name="On"  style="font-size: 20pt; height: 40px; width: 600px" type="submit" value="On"/></div> 
   <div align="center"> <input name="Off" style="font-size: 20pt; height: 40px; width: 600px" type="submit" value="Off"/></div> 
   <div align="center"> <input name="Status" style="font-size: 20pt; height: 40px; width: 600px" type="submit" value="Status"/></div> 


<? 
$pidm = 0; $pidp = 0;
$pidm = exec("pidof mplayer"); 
$pidp = exec("pidof php radio2.php"); 
exec("ps w | grep '[m]player' 2>&1", $output); 
$laut=exec("/volume1/@appstore/AudioStation/bin/volume -g");


if (!ini_read("./radio.ini")) {
  $laut = intval($laut);
  $ini = array("Volume" => array("Vol" => $laut));
  ini_write("./radio.ini",$ini);
  }

$ini = ini_read("./radio.ini");

$aa = $_GET["vol"];
if (isset($_GET["vol"]) && isset($_GET["ls"]) && $aa <> $laut) { 
   $nn = exec("/volume1/@appstore/AudioStation/bin/volume -s $aa");
   $ini["Volume"]["Vol"]=intval($aa);
   ini_write("./radio.ini",$ini);
   $laut=exec("/volume1/@appstore/AudioStation/bin/volume -g");
   }

echo '<div align="center"> <br>Lautstärke: <select name="vol">';
for ( $counter = 10; $counter <= 100; $counter += 10) {
	if ($counter <> $ini["Volume"]["Vol"]) {echo "<option>$counter</option>";}
       else {echo "<option selected>$counter</option>";}
	}
echo "</select>";
echo '<input type="submit" name="ls" value="Setzen"></div>';

if (isset($_GET["On"]) && $_GET["On"] == "On") { 
   if ($pidm > 0) exec("killall mplayer"); 
   if ($pidp > 0) exec("kill $pidp"); 
   exec ("nohup php /volume1/@appstore/radio/radio2.php >/volume1/@appstore/radio/radio2.log &"); 
   sleep(1); 
   $pidm = 0; $pidp = 0;
   $pidm = exec("pidof mplayer"); 
   $pidp = exec("pidof php radio2.php"); 
   $output = "";
   exec("ps w | grep '[m]player' 2>&1", $output); 

} elseif (isset($_GET["Off"]) && $_GET["Off"] == "Off") { 
   if ($pidm > 0) exec("killall mplayer"); 
   if ($pidp > 0) exec("kill $pidp"); 
   sleep(3); 
   $pidm = 0; $pidp = 0;
   $pidm = exec("pidof mplayer"); 
   $pidp = exec("pidof php radio2.php"); 
   $output = "";
   exec("ps w | grep '[m]player' 2>&1", $output); 
} 

?> 

<div style="background-color:white;">  
<? echo "<pre>Lautstärke laut ini : "; echo $ini["Volume"]["Vol"];
   echo  "<br>Lautstärke abgefragt: $laut<br>";
   echo      "Action              : $nn<br>PID(s) mplayer      : $pidm<br>PID radio2.php      : $pidp<br>" ?>
</div> 
<div style="background-color:white;">
<? echo "<small><pre>";print_r($output); echo "</pre></small>"; ?>
</div>
<br> 
<br> 
<br> 
</form>
</body>

<?
/**
 * Write data to an INI file
 * 
 * The data array has to be like this:
 * 
 *  Array
 *  (
 *      [Section1] => Array
 *          (
 *              [key1] => val1
 *              [key2] => val2
 *          )
 *      [Section2] => Array
 *          (
 *              [key3] => val3
 *              [key4] => val4
 *          )    
 *  )
 *
 * @param string $filePath
 * @param array $data
 */
function ini_write($filePath, array $data)
{
    $output = '';
 
    foreach ($data as $section => $values)
    {
        //values must be an array
        if (!is_array($values)) {
            continue;
        }
 
        //add section
        $output .= "[$section]\n";
 
        //add key/value pairs
        foreach ($values as $key => $val) {
            $output .= "$key=$val\n";
        }
        $output .= "\n";
    }
 
    //write data to file
    file_put_contents($filePath, trim($output));
}
 
 
/**
 * Read and parse data from an INI file
 * 
 * The data is returned as follows:
 * 
 *  Array
 *  (
 *      [Section1] => Array
 *          (
 *              [key1] => val1
 *              [key2] => val2
 *          )
 *      [Section2] => Array
 *          (
 *              [key3] => val3
 *              [key4] => val4
 *          )    
 *  )
 * 
 * @param string $filePath
 * @return array|false
 */
function ini_read($filePath)
{
    if (!file_exists($filePath)) {
        return false;
    }
 
    //read INI file linewise
    $lines = array_map('trim', file($filePath));
    $data  = array();
 
    $currentSection = null;
    foreach ($lines as $line)
    {
        if (substr($line, 0, 1) == '[') {
            $currentSection = substr($line, 1, -1);
            $data[$currentSection] = array();
        }
        else
        {
            //skip line feeds in INI file
            if (empty($line)) {
                continue;
            }
 
            //if no $currentsection is still null,
            //there was missing a "[<sectionName>]"
            //before the first key/value pair
            if (null === $currentSection) {
                return false;
            }
 
            //get key and value
            list($key, $val) = explode('=', $line);
            $data[$currentSection][$key] = $val;
        }
    }
 
    return $data;
}
?>

Hintergrund-Script:
Rich (BBCode):
<?
$oldpath = "a";
$scr = "Ha";

while ($scr !== "Ho") {

 $woche = date("W",time());$jahr = date("Y",time());
 $path = "/volume1/music/spots/$jahr$woche*.mp3";
 if ($path !== $oldpath) {
	$cc = 0;
	$play = 0;
	$oldpath = $path;
	if (is_array(glob($path))) {
		foreach (glob($path) as $filename) {
	    		$cc++;
			$play = 1;
			$spots[$cc] = $filename;
			$len[$cc] = exec("ffmpeg -i $filename 2>&1 |grep Duration |awk '{print $2}' |awk -F: '{print ($1 * 3600) + ($2 * 60 ) + ($3)}'");
			$len[$cc] = round($len[$cc] + 0.5);
			}
		}
	print_r($spots); echo "\n"; print_r($len); echo "\n";
	}
 

 if ($cc > 0) {
       $ini = ini_read("./radio.ini");
       $vol = $ini["Volume"]["Vol"];
	$test = exec ("killall mplayer; /var/packages/AudioStation/target/bin/mplayer $spots[$play] -loop 0 -daemon -volume $vol"); 
	$heute = date("Y.m.d H:i");
 	echo "$heute - Now Playing $spots[$play] - ($test)\n";
	sleep($len[$play]);
	$play++;
	if ($play > $cc) {$play = 1;}
	}

 $ini = ini_read("./radio.ini");
 $vol = $ini["Volume"]["Vol"];
 $test = exec ("killall mplayer; /var/packages/AudioStation/target/bin/mplayer http://edge.live.mp3.mdn.newmedia.nacamar.net/ps-antennenr/livestream.mp3.m3u -loop 0 -daemon -volume $vol"); 
 $heute = date("Y.m.d H:i");
 echo "$heute - Now Playing Radio - ($test)\n";
 sleep(1200);
}

/**
 * Write data to an INI file
 * 
 * The data array has to be like this:
 * 
 *  Array
 *  (
 *      [Section1] => Array
 *          (
 *              [key1] => val1
 *              [key2] => val2
 *          )
 *      [Section2] => Array
 *          (
 *              [key3] => val3
 *              [key4] => val4
 *          )    
 *  )
 *
 * @param string $filePath
 * @param array $data
 */
function ini_write($filePath, array $data)
{
    $output = '';
 
    foreach ($data as $section => $values)
    {
        //values must be an array
        if (!is_array($values)) {
            continue;
        }
 
        //add section
        $output .= "[$section]\n";
 
        //add key/value pairs
        foreach ($values as $key => $val) {
            $output .= "$key=$val\n";
        }
        $output .= "\n";
    }
 
    //write data to file
    file_put_contents($filePath, trim($output));
}
 
 
/**
 * Read and parse data from an INI file
 * 
 * The data is returned as follows:
 * 
 *  Array
 *  (
 *      [Section1] => Array
 *          (
 *              [key1] => val1
 *              [key2] => val2
 *          )
 *      [Section2] => Array
 *          (
 *              [key3] => val3
 *              [key4] => val4
 *          )    
 *  )
 * 
 * @param string $filePath
 * @return array|false
 */
function ini_read($filePath)
{
    if (!file_exists($filePath)) {
        return false;
    }
 
    //read INI file linewise
    $lines = array_map('trim', file($filePath));
    $data  = array();
 
    $currentSection = null;
    foreach ($lines as $line)
    {
        if (substr($line, 0, 1) == '[') {
            $currentSection = substr($line, 1, -1);
            $data[$currentSection] = array();
        }
        else
        {
            //skip line feeds in INI file
            if (empty($line)) {
                continue;
            }
 
            //if no $currentsection is still null,
            //there was missing a "[<sectionName>]"
            //before the first key/value pair
            if (null === $currentSection) {
                return false;
            }
 
            //get key and value
            list($key, $val) = explode('=', $line);
            $data[$currentSection][$key] = $val;
        }
    }
 
    return $data;
}

?>
 
Status
Für weitere Antworten geschlossen.
 

Kaffeautomat

Wenn du das Forum hilfreich findest oder uns unterstützen möchtest, dann gib uns doch einfach einen Kaffee aus.

Als Dankeschön schalten wir deinen Account werbefrei.

:coffee:

Hier gehts zum Kaffeeautomat