Coder Social home page Coder Social logo

Comments (1)

Raveler avatar Raveler commented on July 22, 2024 2

Hey,

This is actually the use case I developed this library for, so I can confirm this works.

We were using this library to receive video data frame by frame and record it to a h264-encoded file. In our case, the video data came from a render engine, but it can come from a video, as long as the video data is in a format ffmpeg can understand.

Here's the code we used to write frame by frame from whatever source. EncodeFrame is called with a raw, unencoded RGBA byte array that whenever a frame arrives from the source.

// AniHub.LipSync.cpp : Defines the exported functions for the DLL application.
//

#include "stdafx.h"

#include <string>
#include <iostream>
#include <stdio.h>
#include <io.h>
#include <filesystem>
#include <fstream>
#include <sstream>

using namespace std;
using namespace ffmpegcpp;


struct Context
{
  VideoCodec* codec = nullptr;
  Muxer* muxer = nullptr;
  RawVideoDataSource* source = nullptr;
  VideoFrameSink* encoder = nullptr;
};

void CleanUp(Context* ctx)
{
  if (ctx->muxer != nullptr) delete ctx->muxer;

  if (ctx->source != nullptr) delete ctx->source;
  if (ctx->encoder != nullptr) delete ctx->encoder;

  delete ctx;
}

void* StartExport(const char* outputFileName, int width, int height, int framesPerSecond)
{
  Context* ctx = new Context();
  try
  {
    // create the output muxer
    ctx->muxer = new Muxer(outputFileName);

    // create the codec based on the output extension
    H264NVEncCodec* h264codec = new H264NVEncCodec();
    h264codec->SetPreset("losslesshp");
    ctx->codec = h264codec;

    // create the encoder that will link the source and muxer together
    ctx->encoder = new VideoEncoder(ctx->codec, ctx->muxer);

    // if we need to extract alpha, we need to send the A-channel to the filter
    // and we let the filter take care of the conversion to the final pixel format.
    ctx->source = new RawVideoDataSource(width, height, AV_PIX_FMT_BGRA, AV_PIX_FMT_RGBA, framesPerSecond, ctx->encoder);

    return ctx;
  }
  catch (FFmpegException e)
  {
    CleanUp(ctx);
    return nullptr;
  }
}

int EncodeFrame(void* handle, void* frameData, int bytesPerRow)
{
  Context* ctx = (Context*)handle;

  // push to the source
  try
  {
    ctx->source->WriteFrame(frameData, bytesPerRow);
    return 0;
  }
  catch (FFmpegException e)
  {
    return -1;
  }
}

void Cancel(void* handle)
{
  Context* ctx = (Context*)handle;
  CleanUp(ctx);
}

int Close(void* handle)
{
  Context* ctx = (Context*)handle;
  try
  {
    ctx->muxer->Close();
    CleanUp(ctx);
    return 0;
  }
  catch (FFmpegException e)
  {
    return -1;
  }
}

from ffmpeg-cpp.

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.