C#實現(xiàn)視頻的批量剪輯功能
篇首,完全沒有技術(shù)含量的帖子,高手略過,只為十幾年后重新?lián)炱鸬奈覑酆猛嫱妗!!?/p>
起因,一個朋友說他下載了很多短視頻,但只需要要其中的一小截,去頭掐尾,在軟件里搞來搞去太麻煩,讓我?guī)兔?,我這個編程二吊子爽快的接了下來。
還是一二三理清思路,方案就用ffmpeg,命令行剪輯生成新視頻,c#做個集成一鍵處理。。
一,采用預(yù)置數(shù)據(jù)data.txt,記錄【視頻文件名,起點時間,終止時間】,此為單獨一行,多個文件就多行,如下圖

二,一個videocut類
class VideoCut
{
public string file;
public string begin;
public string end;
public VideoCut(string f,string b,string w)
{
file = f;
begin = b;
end = w;
}
}三,解析數(shù)據(jù)文件data.txt,生成videocut的列表
count = 0;
listbox.Items.Clear();
logno("開始解析數(shù)據(jù)文件....");
if (!System.IO.File.Exists("data.txt"))
{
log("找不到數(shù)據(jù)文件data.txt");
return;
}
List<VideoCut> list = new List<VideoCut>();
string[] ary;
TimeSpan begin;
TimeSpan end;
int i = 0;
foreach (string line in System.IO.File.ReadLines("data.txt"))
{
ary = line.Trim().Split(',');
log("第" + ++i + "行:" + line.Trim());
if(ary.Length!=3)
{
log("數(shù)據(jù):"+line.Trim()+",格式不對");
continue;
}
if (!System.IO.File.Exists(ary[0]))
{
log("文件:"+ary[0].Trim()+",不存在");
continue;
}
if (!TimeSpan.TryParse(ary[1].Trim(), out begin))
{
log("起點時間:" + ary[1].Trim() + ",格式不對");
continue;
}
if (!TimeSpan.TryParse(ary[2].Trim(), out end))
{
log("截止時間:" + ary[2].Trim() + ",格式不對");
continue;
}
if (end <= begin)
{
log("截止時間應(yīng)該大于起點時間!?。。?!");
continue;
}
list.Add(new VideoCut(ary[0], ary[1], (end-begin).ToString()));
}
logno("解析數(shù)據(jù)文件完畢,成功解析文件:"+list.Count+"個...");
if (list.Count < 1)
{
log("沒有數(shù)據(jù),退出");
}
四,一個ffmpeg的剪輯類
class FFMEPG
{
//視頻切割
public static string Cut(string OriginFile/*視頻源文件*/, string startTime/*開始時間*/, string endTime/*結(jié)束時間*/)
{
string DstFile = OriginFile.Replace(".", "a.");
string strCmd = " -ss "+ startTime
+" -i " + OriginFile
+ " -to " +endTime
+ " -vcodec copy -acodec copy " + DstFile + " -y ";
if (System.IO.File.Exists(DstFile))System.IO.File.Delete(DstFile);
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "ffmpeg.exe";//要執(zhí)行的程序名稱
p.StartInfo.Arguments = " " + strCmd;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = false;//可能接受來自調(diào)用程序的輸入信息
p.StartInfo.RedirectStandardOutput = false;//由調(diào)用程序獲取輸出信息
p.StartInfo.RedirectStandardError = false;//重定向標準錯誤輸出
p.StartInfo.CreateNoWindow = false;//不顯示程序窗口
p.Start();//啟動程序
p.WaitForExit();//等待程序執(zhí)行完退出進程
if (System.IO.File.Exists(DstFile))
{
return DstFile;
}
return "";
}
}
五,循環(huán)調(diào)用videocut列表
VideoCut c;
string file;
for (i = 0; i < list.Count; i++)
{
logno("開始剪切第【" +i + "】個文件...");
c=list[i];
file = FFMEPG.Cut(c.file, c.begin, c.end);
if (file.Length > 0)
{
log("剪切成功,輸出文件:"+file);
}
else log("剪切失敗.....");
}
log("");
log("");
log("剪切完成......");
六,大致就這樣了,運行如下圖

ffmpeg命令要能夠調(diào)用哈,放到同目錄或都windows系統(tǒng)目錄都行。。。
源代碼已經(jīng)上傳,可以下載到。。。
到此這篇關(guān)于C#實現(xiàn)視頻的批量剪輯的文章就介紹到這了,更多相關(guān)C#視頻批量剪輯內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
.NET中的async和await關(guān)鍵字使用及Task異步調(diào)用實例
這篇文章主要介紹了.NET中的async和await關(guān)鍵字使用及Task異步調(diào)用實例,本文還包含了取消執(zhí)行和顯示進度的例子,需要的朋友可以參考下2014-07-07
C# IDE VS2005中的Hosting Process (vshost.exe)作用介紹
這篇文章主要介紹了C# IDE VS2005中的Hosting Process (vshost.exe)作用介紹,vshost.exe是一個宿主進程,主要用來提高調(diào)試效率,需要的朋友可以參考下2015-01-01
c#使用nsoup解析html亂碼解決方法分享 nsoup教程
NSoup是JSoup的Net移植版本。使用方法基本一致。如果項目涉及HTML的處理,強烈推薦NSoup。但是遺憾的是NSoup默認的編碼是UTF-8,處理中文有亂碼,下面給出二種解決方法2014-01-01

