Xogy / xsound

Improved audio library for FiveM
MIT License
115 stars 82 forks source link

Title grapper #25

Closed benzon closed 2 years ago

benzon commented 2 years ago

Function to fetch title of a youtube video, usefully for queueing up multiple tracks and getting there repective titles.

Xogy commented 2 years ago

if you still need the code here is what I made for my another resource but I am not really planning to add this. Just make sure you use howler.

function editString(string){
    var str = string;
    var res = str.split("/");
    var final = res[res.length - 1];
    final = final.replace(".mp3", " ");
    final = final.replace(".wav", " ");
    final = final.replace(".wma", " ");
    final = final.replace(".wmv", " ");

    final = final.replace(".aac", " ");
    final = final.replace(".ac3", " ");
    final = final.replace(".aif", " ");
    final = final.replace(".ogg", " ");
    final = final.replace("%20", " ");
    final = final.replace("-", " ");
    final = final.replace("_", " ");
    return final;
}

function getNameFromURL(URL, cb){
    if(getYoutubeUrlId(URL) === "")
    {
        var audioPlayer = new Howl({
                src: [URL],
                loop: false,
                html5: true,
                autoplay: true,
                volume: 0.0,
                format: ['mp3'],
                onplay: function(){
                    cb(editString(URL), audioPlayer._duration);
                    audioPlayer.pause();
                    audioPlayer.stop();
                    audioPlayer.unload();
                },
        });
    }
    else
    {
        var test = new YT.Player("trash",
        {
            height: '0',
            width: '0',
            videoId: getYoutubeUrlId(URL),
            events:
            {
                'onReady': function(event){
                    cb(event.target.getVideoData().title, test.getDuration());
                    test.stopVideo();
                    test.destroy();
                },
            }
        });
    }
}

getNameFromURL("https://www.youtube.com/watch?v=-wZR_q3_4Ag", function(name, timestamp){
    console.log(name);
});