May
26
2019

ffmpeg stream raw video into Unity Texture2D

Small proof-of-concept idea about streaming raw video from commandline ffmpeg into Unity, using UDP streaming. It works ok for small resolution and low fps rate, but breaks/goes out of sync if any udp data goes missing.

Sources:
https://github.com/unitycoder/ffmpegStreamToUnity

Instructions:
– Download ffmpeg https://ffmpeg.org/download.html
– run from commandline: > ffmpeg -f gdigrab -i desktop -pixel_format rgb8 -video_size 256×256 -vf scale=256:256 -framerate 5 -r 5 -f rawvideo udp://127.0.0.1:8888
– Start the above unity project, and it should display received data

Desktop capture streamed into Unity material texture2D

Next steps
– TCP version maybe fixes missed frames/out of sync problems
– Use proper container mpegts or so, to fix issues with udp missed frames etc. but difficult to decode mpeg4 payload data(?)

$$$ Reward $$$
– First one to post working pull-request (or separate repo) for mpegts/mp4/mpeg4 or handle other proper video stream parsing inside unity (without plugins, or only open source plugins) wins 50usd/paypal, 100usd/paypal : D because i think this could be useful open source tool for the community (to be able to easily stream data from ffmpeg)
– Requirements, parse this data into texture2d:
ffmpeg -f gdigrab -i desktop -r 1 -f mpegts udp://127.0.0.1:8888
// or more options
ffmpeg -f gdigrab -i desktop -pixel_format rgb24 -video_size 512×512 -vf scale=512:512 -framerate 1 -r 1 -f mpegts dp://127.0.0.1:8888
// it doesnt have to be plain UDP, can use rtp, tcp or others too, can use different pixel format too


16 Comments + Add Comment

  • Hi mgear, I did something similar for OpenGL where I chose to render each pixel with a colored quad. You might be interested in the program here at http://www.alsprogrammingresource.com/video.html

  • Hi mgear,

    On Windows, have a look at ViveMediaDecoder by HTC
    https://github.com/ViveSoftware/ViveMediaDecoder

    It is Unity Windows software decoder based on FFmpeg. From the code I see that it should support RTSP. If so you should be able to stream directly from FFmpeg or gstreamer.

    On Linux you could take a look at (my) unity-network-hardware-video-decoder

    https://github.com/bmegli/unity-network-hardware-video-decoder

    It will not get you the job done, it uses custom network protocol but you can get the idea how to do it on Linux and other systems (the rendering part is cross platform).

    By combining the rendering part from UNHVD with decoding from Vive you should be able to get cross platform Unity Player.

    By combining the decoding part from UNHVD with Vive, you should be able to get hardware accelerated Unity Player.

    Kind regards

  • this might work? its for mjpeg:
    Components for video streaming in Unity
    https://github.com/gpvigano/VidStreamComp

  • Hi, So I’ve tried a lot of things in order to achieve video streaming over UDP…
    bumped into this thread several times:)

    Eventually, salvation came from the attached repo…. it uses AsyncGPUReadbackRequest which is awesome.
    you do need to change the FFmpeg command from inside the code, but it works perfectly and even harness the HW acceleration for decoding.

    anyway enjoy Brother:
    https://github.com/keijiro/FFmpegOut

    and change is this:

    public static FFmpegSession CreateWithOutputPath(
    string outputPath,
    int width, int height, float frameRate,
    FFmpegPreset preset
    )
    {
    return new FFmpegSession(
    “-y -f rawvideo -vcodec rawvideo -pixel_format rgba”
    + ” -colorspace bt709″
    + ” -video_size ” + width + “x” + height
    + ” -framerate ” + frameRate
    + ” -loglevel warning -i – ” + preset.GetOptions()
    //+ ” \”” + outputPath + “\””
    //+ ” -f h264 udp://127.0.0.1:6000″
    + ” -f h264 udp://192.168.1.39:6000″
    );
    }

    • Hi Ariel,

      Were you able to use this method to stream video using FFMPEG into Unity over UDP? I have been trying to get something like that working correctly, but can’t seem to get anywhere. I am streaming my desktop using FFMPEG and want it to play in Unity (on a flat plane for example).

      Thanks,
      Shane

  • @mgear, would you have any advice on how to change this from a desktop stream to a stream from a wifi camera over UDP? I’m trying to get the direct feed of a Tello drone camera into Unity.

    I’m just starting with ffmpeg but this is what I’ve tried (and failed with) so far:

    ffmpeg -i udp://[drone’s IP and port here] -r 1 -f mpegts udp://127.0.0.1:8888

    This (probably unsurprisingly) did not work.

  • Unity network texture sender and receiver for video and frame streaming
    https://github.com/BarakChamo/uTextureSendReceive

    • Hi mgear,

      This link you sent uses TCP, have you ever been able to get reliable streaming into Unity over UDP? I am using FFMPEG and want to stream over UDP into Unity. I tried your first method and like you said it works well for several seconds then breaks. Any assistance would be appreciated.

      Thanks,
      Shane

      • UDP != reliable, but havent tested further, but i think the issue with my code is that it doesnt have any kind of “sync” method, if even one byte is lost, it goes out of sync. (instead of waiting for something like RTP packaged frame, where you can check frame contents if its corrupt etc.)

  • FMETP STREAM 3.0 has Desktop capture & stream over UDP, it’s compatible with PC Mac and Linux.
    https://youtu.be/ZEOzbVy0UYU?t=36

  • Not the best solution but maybe a hint to reset the buffer.

    if (this.bufferIndex + len > this.bufferSize)
    {
    this.bufferFrameStart = 0;

    var diff = this.bufferSize – this.bufferIndex;
    len -= diff;
    this.bufferIndex = len;
    Array.Copy(this.receivedBytes, 0, this.dumpPixels, this.bufferIndex, len);
    }
    else
    {
    Array.Copy(this.receivedBytes, 0, this.dumpPixels, this.bufferIndex, len);
    this.bufferIndex += len;
    }

Leave a comment

Connect

Twitter View LinkedIn profile Youtube Youtube Join Discord Twitch Instagram

UnityLauncherPro

Get UnityLauncherPRO and work faster with Unity Projects!
*free unity hub alternative

@unitycoder_com

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.