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

二,一個(gè)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()+",格式不對(duì)");
continue;
}
if (!System.IO.File.Exists(ary[0]))
{
log("文件:"+ary[0].Trim()+",不存在");
continue;
}
if (!TimeSpan.TryParse(ary[1].Trim(), out begin))
{
log("起點(diǎn)時(shí)間:" + ary[1].Trim() + ",格式不對(duì)");
continue;
}
if (!TimeSpan.TryParse(ary[2].Trim(), out end))
{
log("截止時(shí)間:" + ary[2].Trim() + ",格式不對(duì)");
continue;
}
if (end <= begin)
{
log("截止時(shí)間應(yīng)該大于起點(diǎn)時(shí)間?。。。?!");
continue;
}
list.Add(new VideoCut(ary[0], ary[1], (end-begin).ToString()));
}
logno("解析數(shù)據(jù)文件完畢,成功解析文件:"+list.Count+"個(gè)...");
if (list.Count < 1)
{
log("沒有數(shù)據(jù),退出");
}
四,一個(gè)ffmpeg的剪輯類
class FFMEPG
{
//視頻切割
public static string Cut(string OriginFile/*視頻源文件*/, string startTime/*開始時(shí)間*/, string endTime/*結(jié)束時(shí)間*/)
{
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;//重定向標(biāo)準(zhǔn)錯(cuò)誤輸出
p.StartInfo.CreateNoWindow = false;//不顯示程序窗口
p.Start();//啟動(dòng)程序
p.WaitForExit();//等待程序執(zhí)行完退出進(jìn)程
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 + "】個(gè)文件...");
c=list[i];
file = FFMEPG.Cut(c.file, c.begin, c.end);
if (file.Length > 0)
{
log("剪切成功,輸出文件:"+file);
}
else log("剪切失敗.....");
}
log("");
log("");
log("剪切完成......");
六,大致就這樣了,運(yùn)行如下圖

ffmpeg命令要能夠調(diào)用哈,放到同目錄或都windows系統(tǒng)目錄都行。。。
源代碼已經(jīng)上傳,可以下載到。。。
到此這篇關(guān)于C#實(shí)現(xiàn)視頻的批量剪輯的文章就介紹到這了,更多相關(guān)C#視頻批量剪輯內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
.NET中的async和await關(guān)鍵字使用及Task異步調(diào)用實(shí)例
這篇文章主要介紹了.NET中的async和await關(guān)鍵字使用及Task異步調(diào)用實(shí)例,本文還包含了取消執(zhí)行和顯示進(jìn)度的例子,需要的朋友可以參考下2014-07-07
C# DropDownList中點(diǎn)擊打開新窗口的方法
C# DropDownList中點(diǎn)擊打開新窗口的方法,需要的朋友可以參考一下2013-03-03
C#添加Windows服務(wù) 定時(shí)任務(wù)
這篇文章主要為大家詳細(xì)介紹了C#添加Windows服務(wù),定時(shí)任務(wù)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
C#利用itext實(shí)現(xiàn)PDF頁面處理與切分
這篇文章主要介紹了如何在C#中使用itext做一個(gè)pdf的頁面大小一致性處理,然后再根據(jù)數(shù)據(jù)切分出需要的pdf,感興趣的小伙伴可以了解一下2022-04-04
C# IDE VS2005中的Hosting Process (vshost.exe)作用介紹
這篇文章主要介紹了C# IDE VS2005中的Hosting Process (vshost.exe)作用介紹,vshost.exe是一個(gè)宿主進(jìn)程,主要用來提高調(diào)試效率,需要的朋友可以參考下2015-01-01
利用C#實(shí)現(xiàn)分布式數(shù)據(jù)庫查詢
利用C#實(shí)現(xiàn)分布式數(shù)據(jù)庫查詢...2007-03-03
c#使用nsoup解析html亂碼解決方法分享 nsoup教程
NSoup是JSoup的Net移植版本。使用方法基本一致。如果項(xiàng)目涉及HTML的處理,強(qiáng)烈推薦NSoup。但是遺憾的是NSoup默認(rèn)的編碼是UTF-8,處理中文有亂碼,下面給出二種解決方法2014-01-01

