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
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
Related Posts
17 Comments + Add Comment
Leave a comment
Recent posts
- MotionVector Effect: Object “disappears” when paused
- [GreaseMonkey] Unity Forum Fixer
- UnityHub: Make Hub application background Translucent
- Customize SpriteShapeRenderer quality (but has issues)
- Editor tool: Copy selected gameobject’s names into clipboard as rows (for Excel)
- Editor tool: Replace string in selected gameobject’s names
- UnityHub: Enable built-in Login Dialog (no more browser login/logout issues!)
- Use TikTok-TTS in Unity (with WebRequest)
- Create Scene Thumbnail Image using OnSceneSaved & OnPreviewGUI
- webgl+javascript TTS
- Using Moonsharp (LUA) + Unity Webgl
- Using 3D gameobject prefabs with Unity Tilemap + NavMesh Surface
Recent Comments
- [Asset Store] Point Cloud Viewer & Tools on
- [Asset Store] Point Cloud Viewer & Tools on
- Vector3 maths for dummies! on
- UnityHub: Make Hub application background Translucent on
- UnityHub: Make Hub application background Translucent on
- Install Android SDK+JDK+NDK for Unity (without AndroidStudio or Unity Hub) on
- Install Android SDK+JDK+NDK for Unity (without AndroidStudio or Unity Hub) on
- [Asset Store] Point Cloud Viewer & Tools on
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
Unity Render Streaming webRTC
https://github.com/Unity-Technologies/UnityRenderStreaming
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.
i’d probably try feeding that camera stream into some standalone streaming server like,
https://gist.github.com/unitycoder/62e2fca5bd00a3b907cfe0a95d04f62d
then you can use some video player plugins from asset store to playback those HLS or RTMP streams from that server.
my test code most certainly would start going out of sync..
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.)
Use Mobile-FFmpeg with Unity
https://sourceexample.com/en/671cf50fddfec13eb5e1/
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;
}
TCP receiver:
https://github.com/unitycoder/ffmpegStreamToUnity/issues/2
https://github.com/fh-joanneum-research-and-design-lab/FFmpegOut/tree/live-streaming
Hi mgear,
The code you provided seems to work but I do have a camera that does RTSP stream and i would like to display that video stream on a quad/plane or maybe on a UI element. Do you have any idea on how to do that? Any help would be appreciated…