溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時(shí)間:2023-03-23 11:46:35 來(lái)源:億速云 閱讀:145 作者:iii 欄目:開(kāi)發(fā)技術(shù)

今天小編給大家分享一下C#如何實(shí)現(xiàn)視頻的批量剪輯功能的相關(guān)知識(shí)點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識(shí),所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來(lái)了解一下吧。

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

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

二,一個(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("開(kāi)始解析數(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í)間!?。。。?quot;);
                    continue;
                }
                list.Add(new VideoCut(ary[0], ary[1], (end-begin).ToString()));
            }
            logno("解析數(shù)據(jù)文件完畢,成功解析文件:"+list.Count+"個(gè)...");
            if (list.Count < 1)
            {
                log("沒(méi)有數(shù)據(jù),退出");
            }

 四,一個(gè)ffmpeg的剪輯類

class FFMEPG
    {
        //視頻切割
        public static string Cut(string OriginFile/*視頻源文件*/, string startTime/*開(kāi)始時(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;//可能接受來(lái)自調(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("開(kāi)始剪切第【" +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)行如下圖

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

 ffmpeg命令要能夠調(diào)用哈,放到同目錄或都windows系統(tǒng)目錄都行。

以上就是“C#如何實(shí)現(xiàn)視頻的批量剪輯功能”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會(huì)為大家更新不同的知識(shí),如果還想學(xué)習(xí)更多的知識(shí),請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI