Comments (3)
You have to set MediaOptions.VideoPixelFormat
to Rgb24
. To fix the flipped image, set texture tiling or scale (-1,1). This is a strange issue that only happens in Unity. You can also allocate memory for decoded video frames once to improve performance.
Simple example:
using UnityEngine;
using FFMediaToolkit;
using FFMediaToolkit.Decoding;
using FFMediaToolkit.Graphics;
using System.Runtime.InteropServices;
using System;
public class VideoPlayerExample : MonoBehaviour
{
Texture2D texture;
MeshRenderer meshRenderer;
IntPtr buffer;
int bufferSize;
int frameWidth;
int frameHeight;
MediaFile videoFile;
bool canReadNextFrame = true;
public string videoURL;
public string ffmpegPath;
void Start()
{
FFmpegLoader.FFmpegPath = ffmpegPath;
FFmpegLoader.LoadFFmpeg();
meshRenderer = GetComponent<MeshRenderer>();
var options = new MediaOptions();
options.VideoPixelFormat = ImagePixelFormat.Rgb24;
videoFile = MediaFile.Open(videoURL, options);
frameWidth = videoFile.Video.Info.FrameSize.Width;
frameHeight = videoFile.Video.Info.FrameSize.Height;
texture = new Texture2D(frameWidth, frameHeight, TextureFormat.RGB24, false);
meshRenderer.material.mainTexture = texture;
meshRenderer.material.mainTextureScale = new Vector2(-1, 1);
bufferSize = frameWidth * frameHeight * 3;
buffer = Marshal.AllocHGlobal(bufferSize);
}
void Update()
{
if (canReadNextFrame)
{
canReadNextFrame = videoFile.Video.TryGetNextFrame(buffer, bufferSize / frameHeight);
texture.LoadRawTextureData(buffer, bufferSize);
texture.Apply();
}
}
private void OnDestroy()
{
Marshal.FreeHGlobal(buffer);
videoFile.Dispose();
}
}
from ffmediatoolkit.
I try it with the code down below,
It worked, but the color is not right and image is upside down.
texture = new Texture2D(1920, 1038, TextureFormat.RGB24, false);
var videoFile = MediaFile.Open(VideoURL);
// Gets the frame at 5th second of the video.
ImageData frame5s = videoFile.Video.GetFrame(TimeSpan.FromSeconds(sec));
byte[] data = frame5s.Data.ToArray();
texture.LoadRawTextureData(data);
texture.Apply();
meshRenderer.material.mainTexture = texture;
rawImage.texture = texture;
from ffmediatoolkit.
Thanks a lot, it worked fine.
from ffmediatoolkit.
Related Issues (20)
- Select Codec By Name
- ImageSharp image -> ImageData ? HOT 2
- AccessViolationException when disposing MediaOutput object
- Encoding ImageData into a stream
- Reset MediaFile Stream position? HOT 5
- How to convert ImageData to SkiaSharp.SkiBitmap HOT 1
- Bounty $$ -- FFMediaToolkit.FFmpegException: 'Failed to open video encoder. Error code: -542398533 : Generic error in an external library' HOT 3
- Video.AddFrame(FrameToImageData(ImageData)) causes program to exit
- Adding frames at a variable frame rate
- TryGetFrame set Timeout
- Can FFMediaTookit take advantage of dedicated GPU?
- System.NotSupportedException on MediaBuilder.CreateContainer(@"C:\videos\example.mp4") HOT 3
- How to Reduce high cpu utilization on rtsp streaming
- Adding a range of frames at once to speed up video creation
- Cannot send a packet to the decoder. Error code: -1094995529 : Invalid data found when processing input HOT 5
- Failed to open the media stream HOT 1
- ImageData.Data Stride Estimation
- ImageData.Stride calculation
- System.DllNotFoundException: Cannot load FFmpeg libraries HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ffmediatoolkit.