2023-02-02 21:19:45 +01:00
using System.Drawing ;
2021-03-15 22:45:30 +01:00
using FFMpegCore ;
using FFMpegCore.Enums ;
2022-03-24 20:19:37 +01:00
using FFMpegCore.Extensions.System.Drawing.Common ;
2023-02-02 21:19:45 +01:00
using FFMpegCore.Pipes ;
2023-02-13 11:25:45 +01:00
using SkiaSharp ;
2021-03-15 22:45:30 +01:00
var inputPath = "/path/to/input" ;
var outputPath = "/path/to/output" ;
{
var mediaInfo = FFProbe . Analyse ( inputPath ) ;
}
{
var mediaInfo = await FFProbe . AnalyseAsync ( inputPath ) ;
}
{
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 ( ) ;
}
{
// process the snapshot in-memory and use the Bitmap directly
2022-03-24 20:19:37 +01:00
var bitmap = FFMpegImage . Snapshot ( inputPath , new Size ( 200 , 400 ) , TimeSpan . FromMinutes ( 1 ) ) ;
2021-03-15 22:45:30 +01:00
// or persists the image on the drive
FFMpeg . Snapshot ( inputPath , outputPath , new Size ( 200 , 400 ) , TimeSpan . FromMinutes ( 1 ) ) ;
}
var inputStream = new MemoryStream ( ) ;
var outputStream = new MemoryStream ( ) ;
{
await FFMpegArguments
. FromPipeInput ( new StreamPipeSource ( inputStream ) )
. OutputToPipe ( new StreamPipeSink ( outputStream ) , options = > options
. WithVideoCodec ( "vp9" )
. ForceFormat ( "webm" ) )
. ProcessAsynchronously ( ) ;
}
{
FFMpeg . Join ( @"..\joined_video.mp4" ,
@"..\part1.mp4" ,
@"..\part2.mp4" ,
@"..\part3.mp4"
) ;
}
{
2023-02-02 21:19:45 +01:00
FFMpeg . JoinImageSequence ( @"..\joined_video.mp4" , frameRate : 1 , @"..\1.png" , @"..\2.png" , @"..\3.png" ) ;
2021-03-15 22:45:30 +01:00
}
{
FFMpeg . Mute ( inputPath , outputPath ) ;
}
{
FFMpeg . ExtractAudio ( inputPath , outputPath ) ;
}
var inputAudioPath = "/path/to/input/audio" ;
{
FFMpeg . ReplaceAudio ( inputPath , inputAudioPath , outputPath ) ;
}
var inputImagePath = "/path/to/input/image" ;
{
2023-02-02 21:19:45 +01:00
FFMpeg . PosterWithAudio ( inputPath , inputAudioPath , outputPath ) ;
2023-01-29 22:17:40 +01:00
// or
2023-02-02 21:19:45 +01:00
#pragma warning disable CA1416
2023-02-13 11:25:45 +01:00
using var image = SKBitmap . Decode ( inputImagePath ) ;
2021-03-15 22:45:30 +01:00
image . AddAudio ( inputAudioPath , outputPath ) ;
2023-02-02 21:19:45 +01:00
#pragma warning restore CA1416
2021-03-15 22:45:30 +01:00
}
IVideoFrame GetNextFrame ( ) = > throw new NotImplementedException ( ) ;
{
IEnumerable < IVideoFrame > CreateFrames ( int count )
{
2023-02-02 21:19:45 +01:00
for ( var i = 0 ; i < count ; i + + )
2021-03-15 22:45:30 +01:00
{
yield return GetNextFrame ( ) ; //method of generating new frames
}
}
2021-11-18 14:00:18 +01:00
2021-03-15 22:45:30 +01:00
var videoFramesSource = new RawVideoPipeSource ( CreateFrames ( 64 ) ) //pass IEnumerable<IVideoFrame> or IEnumerator<IVideoFrame> to constructor of RawVideoPipeSource
{
FrameRate = 30 //set source frame rate
} ;
await FFMpegArguments
. FromPipeInput ( videoFramesSource )
. OutputToFile ( outputPath , false , options = > options
. WithVideoCodec ( VideoCodec . LibVpx ) )
. ProcessAsynchronously ( ) ;
}
{
// setting global options
GlobalFFOptions . Configure ( new FFOptions { BinaryFolder = "./bin" , TemporaryFilesFolder = "/tmp" } ) ;
// or
GlobalFFOptions . Configure ( options = > options . BinaryFolder = "./bin" ) ;
2021-11-18 14:00:18 +01:00
2021-03-15 22:45:30 +01:00
// or individual, per-run options
await FFMpegArguments
. FromFileInput ( inputPath )
. OutputToFile ( outputPath )
. ProcessAsynchronously ( true , new FFOptions { BinaryFolder = "./bin" , TemporaryFilesFolder = "/tmp" } ) ;
2021-11-18 14:00:18 +01:00
// or combined, setting global defaults and adapting per-run options
GlobalFFOptions . Configure ( new FFOptions { BinaryFolder = "./bin" , TemporaryFilesFolder = "./globalTmp" , WorkingDirectory = "./" } ) ;
await FFMpegArguments
. FromFileInput ( inputPath )
. OutputToFile ( outputPath )
. Configure ( options = > options . WorkingDirectory = "./CurrentRunWorkingDir" )
. Configure ( options = > options . TemporaryFilesFolder = "./CurrentRunTmpFolder" )
. ProcessAsynchronously ( ) ;
2023-02-02 21:19:45 +01:00
}