欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#實(shí)現(xiàn)視頻的批量剪輯功能

 更新時間:2023年03月23日 10:47:26   作者:xchenbb  
這篇文章主要介紹了C#實(shí)現(xiàn)視頻的批量剪輯功能,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

篇首,完全沒有技術(shù)含量的帖子,高手略過,只為十幾年后重新?lián)炱鸬奈覑酆猛嫱?。?!?/p>

起因,一個朋友說他下載了很多短視頻,但只需要要其中的一小截,去頭掐尾,在軟件里搞來搞去太麻煩,讓我?guī)兔?,我這個編程二吊子爽快的接了下來。

還是一二三理清思路,方案就用ffmpeg,命令行剪輯生成新視頻,c#做個集成一鍵處理。。

一,采用預(yù)置數(shù)據(jù)data.txt,記錄【視頻文件名,起點(diǎn)時間,終止時間】,此為單獨(dú)一行,多個文件就多行,如下圖

二,一個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("起點(diǎn)時間:" + ary[1].Trim() + ",格式不對");
                    continue;
                }
                if (!TimeSpan.TryParse(ary[2].Trim(), out end))
                {
                    log("截止時間:" + ary[2].Trim() + ",格式不對");
                    continue;
                }
                if (end <= begin)
                {
                    log("截止時間應(yīng)該大于起點(diǎn)時間?。。。?!");
                    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;//重定向標(biāo)準(zhǔn)錯誤輸出
            p.StartInfo.CreateNoWindow = false;//不顯示程序窗口
 
            p.Start();//啟動程序
            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 + "】個文件...");
                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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論