Giter Site home page Giter Site logo

vladjerca / ffmpegsharp Goto Github PK

View Code? Open in Web Editor NEW
236.0 25.0 69.0 89.78 MB

FFMpegSharp is a great way to use FFMpeg encoding when writing video applications, client-side and server-side. It has wrapper methods that allow conversion to all web formats: MP4, OGV, TS and methods of capturing screens from the videos.

C# 99.94% PowerShell 0.06%

ffmpegsharp's Introduction

SUPPORT IS BEING DROPPED

THE NEW PORTED REPOSITORY CAN BE FOUND HERE

FFMpegSharp NuGet Badge

Setup

NuGet:

Install-Package FFMpegSharp

FFMpegSharp is a great way to use FFMpeg encoding when writing video applications, client-side and server-side. It has wrapper methods that allow conversion to all web formats: MP4, OGV, TS and methods of capturing screens from the videos.

Getting started

Setup your app config (ffmpeg files can be found in the 'Resources' folder):

  <appSettings>
    <add key="ffmpegRoot" value="C:\ffmpeg\bin\" />
  </appSettings>

FFProbe

FFProbe is used to gather video information

static void Main(string[] args)
{
    string inputFile = "G:\\input.mp4";

    // loaded from configuration
    var video = new VideoInfo(inputFile);

    string output = video.ToString();

    Console.WriteLine(output);
}

Sample output:

Video Path : G:\input.mp4
Video Root : G:\\
Video Name: input.mp4
Video Extension : .mp4
Video Duration : 00:00:09
Audio Format : none
Video Format : h264
Aspect Ratio : 16:9
Framerate : 30fps
Resolution : 1280x720
Size : 2.88 Mb

FFMpeg

Convert your video files to web ready formats:

static void Main(string[] args)
{
    string inputFile = "input_path_goes_here";
    var encoder = new FFMpeg();
    FileInfo outputFile = new FileInfo("output_path_goes_here");

    var video = VideoInfo.FromPath(inputFile);

    // easily track conversion progress
    encoder.OnProgress += (percentage) => Console.WriteLine("Progress {0}%", percentage);

    // MP4 conversion
    encoder.Convert(
        video,
        outputFile,
        VideoType.Mp4,
        Speed.UltraFast,
        VideoSize.Original,
        AudioQuality.Hd,
        true
    );
    // OGV conversion
    encoder.Convert(
        video,
        outputFile,
        VideoType.Ogv,
        Speed.UltraFast,
        VideoSize.Original,
        AudioQuality.Hd,
        true
    );
    // TS conversion
    encoder.Convert(
        video,
        outputFile,
        VideoType.Ts
    );
}

Easily capture screens from your videos:

static void Main(string[] args)
{
    string inputFile = "input_path_goes_here";
    FileInfo output = new FileInfo("output_path_goes_here");

    var video = VideoInfo.FromPath(inputFile);

    new FFMpeg()
        .Snapshot(
            video,
            output,
            new Size(200, 400),
            TimeSpan.FromMinutes(1)
        );
}

Join video parts:

static void Main(string[] args)
{
    FFMpeg encoder = new FFMpeg();

    encoder.Join(
        new FileInfo(@"..\joined_video.mp4"),
        VideoInfo.FromPath(@"..\part1.mp4"),
        VideoInfo.FromPath(@"..\part2.mp4"),
        VideoInfo.FromPath(@"..\part3.mp4")
    );
}

Join image sequences:

static void Main(string[] args)
{
    FFMpeg encoder = new FFMpeg();

    encoder.JoinImageSequence(
        new FileInfo(@"..\joined_video.mp4"),
        1, // FPS
        ImageInfo.FromPath(@"..\1.png"),
        ImageInfo.FromPath(@"..\2.png"),
        ImageInfo.FromPath(@"..\3.png")
    );
}

Strip audio track from videos:

static void Main(string[] args)
{
    string inputFile = "input_path_goes_here",
       outputFile = "output_path_goes_here";

    new FFMpeg()
        .Mute(
            VideoInfo.FromPath(inputFile),
            new FileInfo(outputFile)
        );
}

Save audio track from video:

static void Main(string[] args)
{
    string inputVideoFile = "input_path_goes_here",
       outputAudioFile = "output_path_goes_here";

    new FFMpeg()
        .ExtractAudio(
            VideoInfo.FromPath(inputVideoFile),
            new FileInfo(outputAudioFile)
        );
}

Add audio track to video:

static void Main(string[] args)
{
    string inputVideoFile = "input_path_goes_here",
       inputAudioFile = "input_path_goes_here",
       outputVideoFile = "output_path_goes_here";

    FFMpeg encoder = new FFMpeg();

    new FFMpeg()
        .ReplaceAudio(
            VideoInfo.FromPath(inputVideoFile),
            new FileInfo(inputAudioFile),
            new FileInfo(outputVideoFile)
        );
}

Add poster image to audio file (good for youtube videos):

static void Main(string[] args)
{
    string inputImageFile = "input_path_goes_here",
       inputAudioFile = "input_path_goes_here",
       outputVideoFile = "output_path_goes_here";

    FFMpeg encoder = new FFMpeg();

    ((Bitmap)Image.FromFile(inputImageFile))
        .AddAudio(
            new FileInfo(inputAudioFile),
            new FileInfo(outputVideoFile)
        );

    /* OR */

    new FFMpeg()
        .PosterWithAudio(
            inputImageFile,
            new FileInfo(inputAudioFile),
            new FileInfo(outputVideoFile)
        );
}

Control over the 'FFmpeg' process doing the job:

static void Main(string[] args)
{
    string inputVideoFile = "input_path_goes_here",
       outputVideoFile = "input_path_goes_here";

    FFMpeg encoder = new FFMpeg();

    // start the conversion process
    Task.Run(() => {
        encoder.Convert(new VideoInfo(inputVideoFile), new FileInfo(outputVideoFile));
    });

    // stop encoding after 2 seconds (only for example purposes)
    Thread.Sleep(2000);
    encoder.Stop();
}

Enums

Video Size enumeration:

public enum VideoSize
{
    HD,
    FullHD,
    ED,
    LD,
    Original
}

Speed enumeration:

public enum Speed
{
    VerySlow,
    Slower,
    Slow,
    Medium,
    Fast,
    Faster,
    VeryFast,
    SuperFast,
    UltraFast
}

Audio codecs enumeration:

public enum AudioCodec
{
        Aac,
        LibVorbis
}

Audio quality presets enumeration:

public enum AudioQuality
{
        Ultra = 384,
        Hd = 192,
        Normal = 128,
        Low = 64
}

Video codecs enumeration:

public enum VideoCodec
{
        LibX264,
        LibVpx,
        LibTheora,
        Png,
        MpegTs
}

ArgumentBuilder

Custom video converting presets could be created with help of ArgumentsContainer class:

var container = new ArgumentsContainer();
container.Add(new VideoCodecArgument(VideoCodec.LibX264));
container.Add(new ScaleArgument(VideoSize.Hd));

var ffmpeg = new FFMpeg();
var result = ffmpeg.Convert(container, new FileInfo("input.mp4"), new FileInfo("output.mp4"));

Other availible arguments could be found in FFMpegSharp.FFMPEG.Arguments namespace.

If you need to create your custom argument, you just need to create new class, that is inherited from Argument, Argument<T> or Argument<T1, T2> For example:

public class OverrideArgument : Argument
{
    public override string GetStringValue()
    {
        return "-y";
    }
}

Contributors

License

Copyright © 2018, Vlad Jerca. Released under the MIT license.

ffmpegsharp's People

Contributors

max619 avatar vladjerca avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ffmpegsharp's Issues

so strange about filesize

in FFProbe.cs, I found file size from

info.Size = Math.Round(videoSize + audioSize, 2);

but I think filesize should be fileInfo.Length?

Reading Video file one frame at a time and writing to disk

Hi,

I am reading an mp4 video file one frame at a time. After some manipulation on that frame I have a Bitmap frame that I want to write to an output mp4 file along with the original audio from the source mp4 file. How do I achieve this using your library?

FFMpegSharp.FFMPEG.Exceptions.FFMpegException: Error opening filters!

When using ConvertTo method and any VideoSize is used which does not match VideoSize.Orginal, then it crashes with following exception:

Unbehandelte Ausnahme: FFMpegSharp.FFMPEG.Exceptions.FFMpegException: Error opening filters! bei FFMpegSharp.FFMPEG.FFMpeg.RunProcess(String args, FileInfo output) in C:\Users\n4cer\Documents\Visual Studio 2017\Projects\FFMpegSharp-master\FFMpegSharp\FFMPEG\FFMpeg.cs:Zeile 372. bei FFMpegSharp.FFMPEG.FFMpeg.ToMp4(VideoInfo source, FileInfo output, Speed speed, VideoSize size, AudioQuality aQuality, Boolean multithread) in C:\Users\n4cer\Documents\Visual Studio 2017\Projects\FFMpegSharp-master\FFMpegSharp\FFMPEG\FFMpeg.cs:Zeile 111. bei FFMpegSharp.VideoInfo.ConvertTo(VideoType type, FileInfo output, Speed speed, VideoSize size, AudioQuality audio, Boolean multithread, Boolean tryToPurge) in C:\Users\n4cer\Documents\Visual Studio 2017\Projects\FFMpegSharp-master\FFMpegSharp\VideoInfo.cs:Zeile 229. bei FFMpegSharp.Example.Program.Main(String[] args) in c:\Users\n4cer\Documents\Visual Studio 2017\Projects\FFMpegSharp-master\FFMpegSharp.Example\Program.cs:Zeile 26.

The code looks like this:
outputFile = new FileInfo("test.mp4"); video.ConvertTo(VideoType.Mp4, outputFile, Speed.UltraFast, VideoSize.Hd, AudioQuality.Hd, true, false);

I could reproduce this using another Win10 machine in VS2017.

parsing double fails.

Using double.Parse like you did in this project fails for me (non US OS) because the way its implemented doesnt work on "my culture settings". In the way its implemented the parsing will just delete the separator eg: 123.456 will get 123456 after parsing.
You did things like:
numberData = double.Parse(dict["streams"][vid]["duration"]);
fix goes like this:
numberData = double.Parse(dict["streams"][vid]["duration"], CultureInfo.InvariantCulture);

Implementation of multi-target project

I think it would be useful, if we would create multi-target build for .Net Standart and .Net Framework. To make this library availible under .Net Framework and .Net Core.
For now we have only one dependency that is not supported by Net Standart - System.Drawing
I'm offering to create build targets for .Net Framework. that includes System.Drawing and for .Net Standart, that doesn't

moving Moov atom to beginning

mobile videos have thier metadata headers at the end of file.
I need to pass arguments to FFMPEG move the header to the beginning. Is there a way ???

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.