Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show Twitch.tv stream within a unity3D application

I would like to make an Unity3D Application in which one can watch a current Twitch.tv live stream.

I am not sure if this is possible, for example, with the twitch api (https://github.com/justintv/twitch-api)

I know about the video textures in Unity3D and I know how to use the default twitch api basics but I do not have an idea how to integrate a running twitch stream into my application.

Can someone please give me a hint if this is possible?

Thanks very much and best regards Meph

like image 997
mephist0ss Avatar asked Jan 18 '16 14:01

mephist0ss


People also ask

Can you embed Twitch streams?

Twitch allows you to embed any stream you want with just a few clicks, even if the stream doesn't belong to you. To do this, simply find the stream you want to embed and look for the share icon in the bottom right. Click the share icon, and then under Share via, choose Embed.

How do you do smartcast on Twitch?

First, connect your phone or tablet to the same Wi-Fi network as your Apple TV. Then open Control Center, tap “Screen Mirroring” and select your Apple TV. As soon as you're connected, open the Twitch app and start streaming!

Does Twitch have an API?

The Twitch API provides the tools and data used to develop Twitch integrations. The data models and systems are designed to provide relevant data in an easy, consistent, and reliable way. For the full list of endpoints that you can use in your integration, explore the Twitch API Reference.

Can you multi stream with Twitch studio?

If you want to increase your audience reach, try multistreaming to Twitch and your other favorite platforms. Multistreaming allows you to stream on Twitch and YouTube — or stream on Twitch and Facebook, or all three!


1 Answers

@Ted Bigham is there anyway to do this just using the default unity video player?

Everything you've said so far has worked great! With just chrome, I'm able to open the .m3u8 and download each 1 second .ts file. With just any video player, I can view each video file just fine.

When I try to pass the url through Video Player though, it says "Cannot read file." I even tried replacing .ts with .mp4 since that is a supported format. This works when I test it outside of Unity, but gives me the same error.

Here's my code.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Video;

public class TwitchStreamer : MonoBehaviour
{

    public string m3u8 = "https://video-weaver.atl01.hls.ttvnw.net/v1/playlist/Cq4EuEYkh5IqurC4gy3nHqGhPVtUwH_" +
        "5QcQbCatC5Fhit9qbivMQ2rdh2MS_m_2OuLd3VS2mF0eTrCKrz8YTuDO19mcbIBMJL3BUMz4jnNuU_t-e53V51TOtaN3vcCk9n3Qr" +
        "dP0WyLREnVR0n_30d4PlUjjW4_si5Wr2XuePQ0dPtxP6jnsenKnX56YTLohtCN2-FdfSvHQMdfd0aw68FA4h9wawHoIM9-U6YRmPa" +
        "fDsfdCiZr_iToSR6lZi81VoYPVjt7Ygf7xKwhjlNwrvA5SnsAnWQGIOVt4UjDkNLw-hmNMAr7RT0iiDghKXZY1VI6Tuc-umB1VXYE" +
        "7BH5hHbDfHgB3_IYNb0fjoudtSuaZxISyWoazPrw3AibEO7k1-quhdcjarBTGpIi_dlPEp-yZlQOy98_OZY_tqjk8ZWTBIaAAYEG_" +
        "miwqsgH4d6eIfkh3ehyMvPQH1C5dVG9tQcSWPUYU6D6hWhxvJhEr-UC0_BYWIVzX7z_Uf74FJGIEqSQc0d6igiowdMM_lyD8ZV9BE" +
        "7wqQs3RegMPqux-AOfF-_Q7Ki2MBv9u7D9ZRXMH_cm20bTx5-ShEDRnWMApSfXK-9bAGNXUcw8YlBbHYeSN5VxEZMC2oGjcivBsGs" +
        "RPMTQ_yNBSM1S6GxFRIR4nqA-mbdXg3rXMW3V6MNybBb1lrrQeEqF1tdYE0rfxe3Ki5WWkxeKmSjMGbMl1tHCwMaReTYkQnX5Qhjl" +
        "HXXtKLtEIEEhB3cXW3oF05-E_q87s68JQIGgyIEKPiQlTsANR9zRc.m3u8";


    public float updateClip = 1f, updateFile = 6f;

    public VideoPlayer vp;

    Queue<string> urls;

    void OnEnable()
    {
        StartCoroutine(UpdateFile());
        //InvokeRepeating("UpdateFile", 0f, updateFile);
        InvokeRepeating("UpdateClip", updateClip, updateClip);
        urls = new Queue<string>();
        vp.prepareCompleted += Vp_prepareCompleted;
        vp.errorReceived += Vp_errorReceived;
    }

    private void Vp_errorReceived(VideoPlayer source, string message)
    {
        Debug.Log("4567: Playback preparation failed. " + message, source);
    }

    private void Vp_prepareCompleted(VideoPlayer source)
    {
        Debug.Log("4567: Playback preparation complete.");
        source.Play();
    }

    void OnDisable()
    {
        CancelInvoke();
        StopAllCoroutines();
        vp.prepareCompleted -= Vp_prepareCompleted;
        vp.errorReceived -= Vp_errorReceived;
    }

    void UpdateClip()
    {
        //vp.Stop();
        if (urls.Count > 0)
        {
            vp.url = urls.Dequeue();
            vp.Prepare();
            Debug.Log("4567: Prepare playback for " + vp.url);
        }
    }

    IEnumerator UpdateFile()
    {
        UnityWebRequest www = UnityWebRequest.Get(m3u8);
        yield return www.SendWebRequest();

        if (www.isNetworkError || www.isHttpError)
        {
            Debug.Log(www.error);
        }
        else
        {
            // Show results as text
            //Debug.Log(www.downloadHandler.text);
            string[] raw = www.downloadHandler.text.Split('\n');
            foreach (string line in raw)
            {
                if (line.Contains("http"))
                {
                    string cnvrt = line.Replace(".ts", ".mp4");
                    urls.Enqueue(cnvrt);
                    Debug.Log(line + " has been added to stream queue.");
                }
            }
            // Or retrieve results as binary data
            //byte[] results = www.downloadHandler.data;
        }
        yield return new WaitForSeconds(updateFile);
    }
}
like image 175
A20T3M4 Avatar answered Sep 28 '22 00:09

A20T3M4