Getting started with Video Edit SDK .Net
Create a new project
The SDK supports many platforms and UI frameworks. You can use Visual Studio or Rider, and create a new project with the required platform and UI framework.
Check the installation guide to add required NuGet packages and native dependencies.
Create the core video editing object
The SDK provides a core editing object that you can use to create a video editing application. To create an object, follow these steps:
- VideoEditCore
- VideoEditCoreX
private VideoEditCore core;
core = new VideoEditCore(VideoView1 as IVideoView);
private VideoEditCoreX core;
core = new VideoEditCoreX(VideoView1 as IVideoView);
Specify the Video View object as a parameter to get the video preview.
Error handling
To handle errors, you can use the OnError
event:
core.OnError += Core_OnError;
private void Core_OnError(object sender, ErrorsEventArgs e)
{
Debug.WriteLine("Error: " + e.Message);
}
Progress handling
To handle progress, you can use the OnProgress
event:
core.OnProgress += Core_OnProgress;
private void Core_OnProgress(object sender, ProgressEventArgs e)
{
Debug.WriteLine("Progress: " + e.Progress);
}
Stop event handling
To handle the stop event, you can use the OnStop
event:
core.OnStop += Core_OnStop;
private void Core_OnStop(object sender, EventArgs e)
{
Debug.WriteLine("Editing completed");
}
Setting basic timeline parameters
Before adding sources, you need to set the frame rate and resolution, depending on the engine.
- VideoEditCore
- VideoEditCoreX
core.Video_FrameRate = new VideoFrameRate(30);
core.Output_VideoSize = new VisioForge.Core.Types.Size(1920, 1080);
core.Output_VideoFrameRate = new VideoFrameRate(30);
Adding video and audio Sources
You can add video and audio sources to the Timeline using the API methods. For each source, you can specify the start and end time and the position on the timeline. You can add video and audio sources independently or together.
Add a video file Source
Many video formats are supported, including MP4, AVI, MOV, WMV, and others.
First create the video source object and set the source file path. You can set the start and end time of the source file in the constructor. Use null to add the whole file.
If the file has multiple video streams, you can specify the stream number.
- VideoEditCore
- VideoEditCoreX
var videoFile = new VisioForge.Core.Types.VideoEdit.VideoSource(
filename,
null,
null,
VideoEditStretchMode.Letterbox,
0,
1.0);
API:
public VideoSource(
string filename,
TimeSpan? startTime = null,
TimeSpan? stopTime = null,
VideoEditStretchMode stretchMode = VideoEditStretchMode.Letterbox,
int streamNumber = 0,
double rate = 1.0)
var videoFile = new VisioForge.Core.Types.X.VideoEdit.VideoFileSource(
filename,
null,
null,
0,
1.0);
API:
public VideoFileSource(
string filename,
TimeSpan? startTime = null,
TimeSpan? stopTime = null,
int streamNumber = 0,
double rate = 1.0)
To play the file faster or slower you can set the rate parameter. For example, to play the file at 2x speed set the rate to 2.0.
In the alternative constructor you can add several file segments.
- VideoEditCore
- VideoEditCoreX
public VideoSource(
string filename,
FileSegment[] segments,
VideoEditStretchMode stretchMode = VideoEditStretchMode.Letterbox,
int streamNumber = 0,
double rate = 1.0)
public VideoFileSource(
string filename,
FileSegment[] segments,
int streamNumber = 0,
double rate = 1.0)
Add the source to the timeline.
- VideoEditCore
- VideoEditCoreX
await core.Input_AddVideoFileAsync(
videoFile,
null,
0);
The third parameter is a destination video stream number. Use 0 to add the source to the first video stream.
API:
public Task<bool> Input_AddVideoFileAsync(
VideoSource fileSource,
TimeSpan? timelineInsertTime = null,
int targetVideoStream = 0,
int customWidth = 0,
int customHeight = 0)
core.Input_AddVideoFile(
videoFile,
null);
API:
public bool Input_AddVideoFile(
VideoFileSource source,
TimeSpan? insertTime = null)
The second parameter is the timeline position. Use null to add the source at the end of the Timeline.
Adding an audio file Source
Supported audio formats include AAC, MP3, WMA, OPUS, Vorbis and others.
Adding logic and parameters is similar to adding video files. Use null to add the entire file.
- VideoEditCore
- VideoEditCoreX
var audioFile = new VisioForge.Core.Types.VideoEdit.AudioSource(
filename,
startTime: null,
stopTime: null,
fileToSync: string.Empty,
streamNumber: 0,
rate: 1.0);
The fileToSync
parameter is used to synchronize audio with video. If you have a video file and an audio file, you can specify the video file name in this parameter. The audio will be synchronized with the video.
await core.Input_AddAudioFileAsync(
audioFile,
insertTime: null,
0);
var audioFile = new AudioFileSource(
filename,
startTime: null,
stopTime: null);
core.Input_AddAudioFile(
audioFile,
insertTime: null);
Add video + audio file source
You can add video and audio sources together.
- VideoEditCoreX
core.Input_AddAudioVideoFile(
filename,
startTime: null,
stopTime: null,
insertTime: null);
Add an image source
You can add an image to the Timeline. JPG, PNG, BMP, and GIF are supported.
Add the image to the Timeline. You should specify the duration of the image on the Timeline.
- VideoEditCore
- VideoEditCoreX
await core.Input_AddImageFileAsync(
filename,
duration: TimeSpan.FromMilliseconds(2000),
timelineInsertTime: null,
stretchMode: VideoEditStretchMode.Letterbox);
core.Input_AddImageFile(
filename,
duration: TimeSpan.FromMilliseconds(2000),
insertTime: null);
Set output format
SDK can output video/audio in various formats, including MP4, AVI, WMV, MKV, WebM, AAC, MP3 and others.
Use the Output_Format
property to set the output format:
- VideoEditCore
- VideoEditCoreX
var mp4Output = new MP4HWOutput();
core.Output_Format = mp4Output;
var mp4Output = new MP4Output("output.mp4");
core.Output_Format = mp4Output;
Available output formats and sample code are available in the Output Formats section.
Adding video effects
You can add video effects to the Timeline. See the Video Effects guide for more information.
The VideoEditCoreX
engine has its own API to add text overlays. Please see the Text Overlays guide.
Add transitions
Check out the transition usage code sample.
Start processing
Now we're ready to start editing.
- VideoEditCore
- VideoEditCoreX
await core.StartAsync();
core.Start();
During editing, you will receive progress and stop events.