FFMpegCore

repository·main·Indexed 24 days ago

https://github.com/rosenbjerg/ffmpegcore

A .NET Standard wrapper for FFMpeg and FFProbe that provides a fluent API for media analysis, conversion, and common video/audio manipulation tasks. It includes support for runtime binary installation, raw video frame piping, and a variety of encoding presets for codecs like libvpx and libx264.

Tokens
10.8K
Snippets
41
Records
42
Agent score
84%

What's inside FFMpegCore

  1. Work with raw video frames via Input Piping

    main

    Input piping allows you to write video frames directly from program memory (e.g., from a generator or a System.Drawing.Bitmap) without saving them to disk first. This is useful for real-time video generation or on-the-fly conversion.

    Using RawVideoPipeSource

    To use raw frames, implement an IEnumerable<IVideoFrame> and pass it to a RawVideoPipeSource.

    1. Create a frame generator:
    IEnumerable<IVideoFrame> CreateFrames(int count)
    {
        for(int i = 0; i < count; i++)
        {
            yield return GetNextFrame();
        }
    }
    1. Pipe the frames to FFMpeg:
    var videoFramesSource = new RawVideoPipeSource(CreateFrames(64))
    {
        FrameRate = 30
    };
    
    await FFMpegArguments
        .FromPipeInput(videoFramesSource)
        .OutputToFile(outputPath, false, options => options
            .WithVideoCodec(VideoCodec.LibVpx))
        .ProcessAsynchronously();

    Note: If you are using System.Drawing.Bitmap, you can wrap them using the BitmapVideoFrameWrapper class.

    var videoFramesSource = new RawVideoPipeSource(CreateFrames(64))
    {
        FrameRate = 30
    };
    await FFMpegArguments
        .FromPipeInput(videoFramesSource)
        .OutputToFile(outputPath, false, options => options
            .WithVideoCodec(VideoCodec.LibVpx))
        .ProcessAsynchronously();
  2. Configure FFMpeg binary paths

    main

    If FFMpeg is not in your system PATH, you must configure the library to find the binaries using GlobalFFOptions or a configuration file.

    Using GlobalFFOptions

    You can set global paths for the binary folder and temporary files folder:

    // Using a new FFOptions object
    GlobalFFOptions.Configure(new FFOptions { BinaryFolder = "./bin", TemporaryFilesFolder = "/tmp" });
    
    // Using a lambda for configuration
    GlobalFFOptions.Configure(options => options.BinaryFolder = "./bin");

    Using a configuration file

    You can provide a ffmpeg.config.json file in your project root. This is read only on the first use:

    {
      "BinaryFolder": "./bin",
      "TemporaryFilesFolder": "/tmp"
    }

    Supporting 32-bit and 64-bit architectures

    To support multiple architectures, organize your BinaryFolder as follows:

    • ./bin/x64/ffmpeg.exe
    • ./bin/x86/ffmpeg.exe

    The library will automatically attempt to use the folder corresponding to the current process architecture.

    GlobalFFOptions.Configure(new FFOptions { BinaryFolder = "./bin", TemporaryFilesFolder = "/tmp" });
  3. Install FFMpegCore and its binaries

    main

    FFMpegCore is a .NET Standard wrapper for FFMpeg/FFProbe. To use it, you must have the FFMpeg binaries available on your system.

    Runtime Auto Installation

    You can automatically download the FFMpeg suite at runtime using the FFMpegDownloader utility:

    FFMpegDownloader.DownloadFFMpegSuite();

    Manual Installation

    Alternatively, you can install FFMpeg manually using your OS package manager:

    • Windows (Chocolatey): choco install ffmpeg -y
    • Mac OSX (Homebrew): brew install ffmpeg mono-libgdiplus
    • Ubuntu: sudo apt-get install -y ffmpeg libgdiplus
    FFMpegDownloader.DownloadFFMpegSuite();
  4. Use FFMpeg helper methods for common tasks

    main

    The FFMpeg class provides high-level helper methods for frequent media operations.

    Snapshots and GIFs

    • Capture a bitmap snapshot: var bitmap = FFMpeg.Snapshot(inputPath, new Size(200, 400), TimeSpan.FromMinutes(1));
    • Save a snapshot to disk: FFMpeg.Snapshot(inputPath, outputPath, new Size(200, 400), TimeSpan.FromMinutes(1));
    • Capture a GIF: await FFMpeg.GifSnapshotAsync(inputPath, outputPath, new Size(480, -1), TimeSpan.FromSeconds(10)); (Use -1 in Size to maintain aspect ratio).

    Video Manipulation

    • Join video parts: FFMpeg.Join(outputPath, part1, part2, part3);
    • Create a sub-video (trim): FFMpeg.SubVideo(inputPath, outputPath, start, end);
    • Join image sequence into video: FFMpeg.JoinImageSequence(outputPath, frameRate, imageInfo1, imageInfo2, ...);

    Audio Operations

    • Mute video: FFMpeg.Mute(inputPath, outputPath);
    • Extract audio: FFMpeg.ExtractAudio(inputPath, outputPath);
    • Replace audio: FFMpeg.ReplaceAudio(inputPath, inputAudioPath, outputPath);
    • Combine image and audio (Poster): FFMpeg.PosterWithAudio(imagePath, audioPath, outputPath);
    // Example: Create a sub video
    FFMpeg.SubVideo(inputPath, 
        outputPath,
        TimeSpan.FromSeconds(0),
        TimeSpan.FromSeconds(30)
    );
  5. Convert media files with FFMpegArguments

    main

    Use the FFMpegArguments fluent API to build complex conversion commands. This allows you to specify codecs, bitrates, video filters, and more.

    Example: Convert to H.264/AAC for web playback

    FFMpegArguments
        .FromFileInput(inputPath)
        .OutputToFile(outputPath, false, options => options
            .WithVideoCodec(VideoCodec.LibX264)
            .WithConstantRateFactor(21)
            .WithAudioCodec(AudioCodec.Aac)
            .WithVariableBitrate(4)
            .WithVideoFilters(filterOptions => filterOptions
                .Scale(VideoSize.Hd))
            .WithFastStart())
        .ProcessSynchronously();

    Example: Stream-to-Stream conversion

    You can use FromPipeInput and OutputToPipe to process media via streams without intermediate files.

    await FFMpegArguments
        .FromPipeInput(new StreamPipeSource(inputStream))
        .OutputToPipe(new StreamPipeSink(outputStream), options => options
            .WithVideoCodec("vp9")
            .ForceFormat("webm"))
        .ProcessAsynchronously();
  6. Use the libvpx-360p preset configuration

    main

    The libvpx-360p.ffpreset file defines a set of encoding parameters optimized for the libvpx video codec at 360p resolution. These settings can be used to configure FFmpeg encoding behavior via the FFMpegCore library.

    Key Encoding Parameters:

    • Codec: vcodec=libvpx
    • GOP Size: g=120
    • Lag-in-frames: lag-in-frames=16
    • Deadline: deadline=good
    • CPU Usage: cpu-used=0 (lower values typically indicate higher quality/slower encoding)
    • Bitrate Control: b=768k (target bitrate)
    • Rate Limits: maxrate=1.5M and minrate=40k (Note: these are ignored unless using -pass 2)
    • Advanced Features: Includes settings for auto-alt-ref and arnr (Adaptive Range Noise Reduction).
    vcodec=libvpx
    
    g=120
    lag-in-frames=16
    deadline=good
    cpu-used=0
    vprofile=0
    qmax=63
    qmin=0
    b=768k
    
    #ignored unless using -pass 2
    maxrate=1.5M
    minrate=40k
    auto-alt-ref=1
    arnr-maxframes=7
    arnr-strength=5
    arnr-type=centered
  7. Use the libvpx-1080p50_60 preset configuration

    main

    This preset provides optimized encoding settings for the libvpx video codec targeting 1080p resolution at 50 or 60 frames per second. It is designed for high-quality encoding using specific GOP structures and rate control parameters.

    Note: The maxrate and minrate parameters are ignored unless performing a two-pass encoding (-pass 2).

    vcodec=libvpx
    
    g=120
    lag-in-frames=25
    deadline=good
    cpu-used=0
    vprofile=1
    qmax=51
    qmin=11
    slices=4
    b=2M
    
    #ignored unless using -pass 2
    maxrate=24M
    minrate=100k
    auto-alt-ref=1
    arnr-maxframes=7
    arnr-strength=5
    arnr-type=centered
  8. Reference the libx264-veryslow_firstpass preset configuration

    main

    The libx264-veryslow_firstpass.ffpreset file defines a specific set of encoding parameters for the libx264 codec. This preset is optimized for a 'veryslow' first pass, likely intended to provide high-quality analysis for a subsequent second pass.

    When using FFMpegCore with this preset, the following key-value pairs are applied to the underlying FFmpeg command:

    coder=1
    flags=+loop
    cmp=+chroma
    partitions=-parti8x8-parti4x4-partp8x8-partb8x8
    me_method=dia
    subq=2
    me_range=24
    g=250
    keyint_min=25
    sc_threshold=40
    i_qfactor=0.71
    b_strategy=2
    qcomp=0.6
    qmin=10
    qmax=51
    qdiff=4
    bf=8
    refs=1
    directpred=3
    trellis=0
    flags2=+bpyramid-mixed_refs+wpred-dct8x8+fastpskip
    wpredp=2
    rc_lookahead=60
  9. Use the libx264-veryfast preset configuration

    main

    The libx264-veryfast.ffpreset file defines a specific set of FFmpeg encoding parameters optimized for the libx264 coder using the veryfast preset logic. These settings balance encoding speed and quality by adjusting motion estimation methods, partition sizes, and rate control parameters. This configuration can be used when passing custom preset parameters to FFmpeg via FFMpegCore to achieve specific performance characteristics.

    coder=1
    flags=+loop
    cmp=+chroma
    partitions=+parti8x8+parti4x4+partp8x8+partb8x8
    me_method=hex
    subq=2
    me_range=16
    g=250
    keyint_min=25
    sc_threshold=40
    i_qfactor=0.71
    b_strategy=1
    qcomp=0.6
    qmin=10
    qmax=51
    qdiff=4
    bf=3
    refs=1
    directpred=1
    trellis=0
    flags2=+bpyramid-mixed_refs+wpred+dct8x8+fastpskip
    wpredp=0
    rc_lookahead=10