Giter Site home page Giter Site logo

Comments (3)

radek-k avatar radek-k commented on June 12, 2024 1

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.

VictorZhanUnity avatar VictorZhanUnity commented on June 12, 2024

I try it with the code down below,
It worked, but the color is not right and image is upside down.

image

        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.

VictorZhanUnity avatar VictorZhanUnity commented on June 12, 2024

Thanks a lot, it worked fine.

from ffmediatoolkit.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.