Jump to content

Play music from Online Radio/YouTube in car.


Tobo

Recommended Posts

Hello, I am new to MTA server making and scripting. I need some script which will play music in car and my friend would hear it. More helpful will be if someone will send file because i don't know scripting very well. Thank for answers! :)

Link to comment
  • MTA Anti-Cheat Team

I would recommend this resource: https://community.mtasa.com/?p=resources&s=details&id=18330

Btw, the recent demise of such scripts that can play YouTube songs right in the speaker is due to YouTube (Google) starting a campaign against API's enabling that feature.. you would need to use your own API for that purpose, and they are now rate-limiting and constantly breaking it, because Google doesn't like that (obviously - if you only grab the audio and not the video with ads, they don't make money from your players streaming it).

Even my resource above used to have a YouTube feature, it's removed due to that but looked like this:

if string.find(url, "youtube") then
  url = "http://yt-mp3cdn.mydomain.com/api/index.php?url=" .. url .. ""
end

inside the "clickEvent" function. So it would forward any YouTube video URL (put in by player) to the API, which will return converted audio to the speaker.

The API could look like either of these:

<?php
$url = $_GET["url"];
header("Content-Type: application/octet-stream");
passthru("youtube-dl -f 'bestaudio[ext=m4a]' -o - '$url' --add-metadata --metadata-from-title '%(artist)s - %(title)s'");
?>

or

<?php
// CONVERTER 2 main
// Don't use any commented code as long the uncommented (latest) script works. A lot of them are old tests and api garbage

// VARS
@$url = $_GET['url'];

$time_start = microtime(true); 



// remove specials chars from url
$vowels = array(":", ".", "=", "/", "?", "&");
$id = str_replace($vowels, "", "$url");
$filename = "./db/$id.txt";

// create upload function
function uploadFTP($server, $username, $password, $local_file, $remote_file){
    // connect to server
    $connection = ftp_connect($server);
	ftp_pasv($connection, true);

    // login
    if (@ftp_login($connection, $username, $password)){
        // successfully connected
    }else{
        return false;
    }

    ftp_put($connection, $remote_file, $local_file, FTP_BINARY);
    ftp_close($connection);
    return true;
}



if (file_exists($filename)) {
	// remove specials chars from url

    $data = file_get_contents("./db/$id.txt");
	@list($id) = explode("|", $data);
	
	

	//echo 'time read cache and redirect: ' . (microtime(true) - $time_start);
	
	
	//echo "cached";
	header("Location: http://mp3.mydomain.com/$id.mp3");
	die();
} else {
    // api stuff
	//echo "access api";
	

// the core
$output = shell_exec("/usr/local/bin/youtube-dl -6 --format bestaudio --metadata-from-title '%(artist)s - %(title)s' -i --add-metadata --max-filesize 64M --max-downloads 1 --no-playlist '$url' -x --prefer-ffmpeg --audio-format mp3 --audio-quality 2 -o './temp/$id.%(ext)s'");

//echo 'time convert song: ' . (microtime(true) - $time_start);

//$output = shell_exec("/usr/local/bin/youtube-dl --postprocessor-args '-threads 2'  --metadata-from-title '%(artist)s - %(title)s' -4 -i --add-metadata --max-filesize 256M --max-downloads 1 --no-playlist '$url' -x --prefer-ffmpeg --audio-format mp3 --audio-quality 128K -o './temp/$id.%(ext)s'");
//uploadFTP("mp3storage.mydomain.com", "YOUTUBECONVERTER", "USERNAME", "./temp/$id.mp3", "/$id.mp3");

//echo 'time upload file: ' . (microtime(true) - $time_start);

//create db
$myfile = fopen("./db/$id.txt", "w") or die("No access or file is gone!");
$txt = "$id";
fwrite($myfile, $txt);
fclose($myfile);	
//echo 'time update db: ' . (microtime(true) - $time_start);

// if doesnt exist, go to backup server
$filename = "./temp/$id.mp3";

if (file_exists($filename)) {
    //uploadFTP("mp3storage.mydomain.com", "YOUTUBECONVERTER", "USERNAME", "./temp/$id.mp3", "/$id.mp3");
uploadFTP("storage.bunnycdn.com", "mp3storage", "mypassword", "./temp/$id.mp3", "/mp3storage/$id.mp3");

	unlink("./temp/$id.mp3");
	
	//echo "api";
header("Location: http://mp3.mydomain.com/$id.mp3");
die();
} else {
	
	
	//order your own CDN with a provider like "bunnycdn (b-cdn)" as below - "CustomYTconverterCDN" is fictive
    header("Location: https://CustomYTconverterCDN.b-cdn.net/api/index.php?url=$url");
die();
}


}

?>

You can even remove all CDN stuff and store everything locally. Otherwise, think about a reverse proxy for CDN (you convert and upload it). With a CDN, the rate limiting problem mentioned at the beginning might not even be present, but I can't tell due to not having recently used any of this myself. Like, i don't even know if the first PHP API code example would still work today.

There's not much (working) "YouTube player" resources that are public on MTA, because to my knownledge a public CDN / converter service that you can use like this is hard to find, they are occcasionally out there - but it's due to that the resource author would need to create and maintain a private CDN and spend traffic on tons of players from the servers that will use their resource. To possibly get rate-limited by YouTube, and it will only cost them.. this is why you usually see such a feature on individual servers only, with custom speaker scripts.

Anyways, credits for much of this API stuff go to @Pilovali and he might be willing to tell you more about it. If you don't care about the entire YouTube feature too much, then again I would recommend you to just use my resource for all other non-YouTube streams (such as internet radio, and direct download links, such as ending with .mp3, of custom songs by players)

With that resource, players can download a music file from anywhere on the internet, go to "Downloads" tab (e.g in Google Chrome) to get the full download link, and paste that into the /music panel that is the resource, to play it to everyone else close to them.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...