Streaming Music Web-server pt7

Streaming Music Web-server pt7

So again focusing on small chunks we will create another piece for our music server. Again remember DO NOT reinvent the wheel.

We are going to create a file called ffmpeg.php and then run it on our server. Here is the code:

<?php
	//get information from a file
	$output = shell_exec('ffmpeg -i bensound-ukulele.mp3 2>&1');
	
	//Output the information to the screen
	echo "<pre>$output</pre>";

	
	//Store the values of the output
	$var_str = var_export($output, true);

	//Save the output to a file name file.txt
	file_put_contents('file.txt', $var_str);
?>

Your output should be:

You can study the comments in the script but the key take away is that we read the file on the server and output it to the screen and a text file.

So this is a good start but let us make it a little better. Create another file but this time call it ffmpeg2.php. Here is the code:

<?php

	//Display the name of the file we are passing
	echo htmlspecialchars($_GET["name"]);

	//Perform a ffmpeg scan of the file name
	$output = shell_exec('ffmpeg -i "' . htmlspecialchars($_GET["name"]) . '" 2>&1');

	//Output the results to the screen
	echo "<pre>$output</pre>";

	//Store the values of the output
	$var_str = var_export($output, true);

	//Save the output to a file name file.txt
	file_put_contents('file.txt', $var_str);

?>

Open up your web browser and enter the url + ?name=bensound-ukulele.mp3. See sample output below

So remember it is always a good idea to read the comments from a developer to get insight on the code. Which means when you code you should add comments in case others will one day view your work.

The difference with the second code is that we pass the name of the file we want to read. Everything else remains the same. So for homework add a few mp3 files to your server directory and then use the name in the URL to read the information.

If you have any questions or comments or even suggestions please do not hesitate to post in the comment section 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *