Application of video transcoding in real project
Thefollowing is the use of the Ffmpeg.exe as transcoding tool, as it was previously encountered in the project to convert . flv file video to . mp4.
1: Next need to prepare tools;http://ffmpeg.org/ Download the corresponding version of Ffmpeg.exe ( +32 bit )
2: Implementation code, the following is just one way to implement:
Using System;
Using System.Collections.Generic;
Using System.Diagnostics;
Using System.Linq;
Using System.Web;
Namespace Videoconvert.models
{
public class Ffmpeghelper
{
<summary>
32 Guests
</summary>
private static string FfmpegPath32 = AppDomain.CurrentDomain.BaseDirectory + @ "Ffmpeg\32\ffmpeg.exe";
<summary>
64 guests
</summary>
private static string ffmpegPath64 = AppDomain.CurrentDomain.BaseDirectory + @ "Ffmpeg\64\ffmpeg.exe";
<summary>
Actual version
</summary>
private static string path;
private static Process p = null;
<summary>
Transcode FLV to MP4
</summary>
<param name= "OldPath" >....flv</param>
<param name= "NewPath" >.....mp4</param>
public void VideoConvert (String oldpath,string newpath)
{
using (p = new Process ())
{
For more transcoding methods, you can view official documents, which are just one of the following:
string arg = "-I" + OldPath + "-c:v libx264-crf 23-c:a libfaac-q:a" + NewPath;
To determine the system version:
Is32or64 ();
p.startinfo.arguments = arg;
p.StartInfo.FileName = path;
P.startinfo.redirectstandarderror = true;
P.startinfo.redirectstandardinput = true;
P.startinfo.redirectstandardoutput = true;
Indicates that the transcoding window is not displayed
P.startinfo.createnowindow = true;
P.startinfo.useshellexecute = false;
Sets the event to be triggered when the process terminates;
p.EnableRaisingEvents = true;
p.exited + = new EventHandler (p_exited);
P.outputdatareceived +=new Datareceivedeventhandler (p_outputdatareceived);
P.errordatareceived +=new Datareceivedeventhandler (p_errordatareceived);
P.start ();
Read output;
P.beginoutputreadline ();
P.beginerrorreadline ();
Set the wait process to trigger the P_exited event and proceed down;
p.WaitForExit ();
}
}
void P_errordatareceived (object sender, Datareceivedeventargs e)
{
Logging output logs
}
void P_outputdatareceived (object sender, Datareceivedeventargs e)
{
Logging output logs
}
void P_exited (object sender, EventArgs e)
{
The process exits triggering the event, which can be used to perform other operations or to determine
}
public void Is32or64 ()
{
if (Environment.is64bitoperatingsystem)
{
Path = ffmpegPath64;
}
else {
Path = FfmpegPath32;
}
}
}
}
Application of video transcoding in real project