簡體   English   中英

使用 ffmpeg 在 c# asp.net 中合並視頻

[英]Merge videos in c# asp.net using ffmpeg

是否可以在 ffmpeg 的幫助下通過 c# asp.net 合並兩個視頻。 在 ffmpeg 文檔中,他們給了我們 cat 命令。 但它不會在asp.net 中工作。 我以為它只適用於linux。

cat intermediate1.mpg intermediate2.mpg > intermediate_all.mpg

asp.net 執行此命令但沒有輸出。 幫我。

namespace demo
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Button2_Click(object sender, EventArgs e)
        {
            string strFile = "cars1.flv";
            MergeFiles(strFile);
        }

        public void MergeFiles(string strFile)
        {
            string strParam;
            string Path_FFMPEG = Server.MapPath("~/ffmpeg/bin/ffmpeg.exe");

            //Converting a video into mp4 format
            string strOrginal = Server.MapPath("~/Videos/");
            strOrginal = strOrginal + strFile;
            string strConvert = Server.MapPath("~/Videos/ConvertedFiles/");
            string strExtn = Path.GetExtension(strOrginal);
            if (strExtn != ".mp4")
            {
                strConvert = strConvert + strFile.Replace(strExtn, ".mp4");
                strParam  = "-i " + strOrginal + " " + strConvert;
                //strParam = "-i " + strOrginal + " -same_quant " + strConvert;
                process(Path_FFMPEG, strParam);
            }

            //Merging two videos               
            String video1 = Server.MapPath("~/Videos/Cars1.mp4");
            String video2 = Server.MapPath("~/Videos/ConvertedFiles/Cars2.mp4");
            String strResult = Server.MapPath("~/Videos/ConvertedFiles/Merge.mp4");
            //strParam = "-loop_input -shortest -y -i " + video1 + " -i " + video2 + " -acodec copy -vcodec mjpeg " + strResult;

            strParam = " -i " + video1 + " -i " + video2 + " -acodec copy -vcodec mjpeg " + strResult;

            process(Path_FFMPEG, strParam);
        }

        public void process(string Path_FFMPEG, string strParam)
        {
            try
            {
                Process ffmpeg = new Process();
                ProcessStartInfo ffmpeg_StartInfo = new ProcessStartInfo(Path_FFMPEG, strParam);
                ffmpeg_StartInfo.UseShellExecute = false;
                ffmpeg_StartInfo.RedirectStandardError = true;
                ffmpeg_StartInfo.RedirectStandardOutput = true;
                ffmpeg.StartInfo = ffmpeg_StartInfo;
                ffmpeg_StartInfo.CreateNoWindow = true;
                ffmpeg.EnableRaisingEvents = true;
                ffmpeg.Start();
                ffmpeg.WaitForExit();
                ffmpeg.Close();
                ffmpeg.Dispose();
                ffmpeg = null;
            }
            catch (Exception ex)
            {

            }
        }

    }
}

終於,我找到了自己的問題的答案。

以下方法解決了我的問題。

public void MergeFiles(string strFile)
        {
            Process p = new Process();
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.FileName = Server.MapPath("~/app.bat");
            p.Start();
            p.WaitForExit();
            p.Dispose();
        }

蝙蝠

app.bat包含以下代碼。

copy e:\cars1.mpg /b + e:\cars2.mpg /b e:\output.mpg /b

注意:這僅適用於Windows。 這就是為什么“ cat”命令不起作用的原因。 代替“ cat”命令,我們使用復制命令。

創建應用程序。 Bat 文件是文件大小的兩倍,但它作為單個文件播放。 還是問題沒有解決。 您應該嘗試 ffmpeg concat 命令方法,在該方法中您需要創建一個包含文件名的文本文件,並且該文件應位於 mp4 文件所在的同一文件夾中。 下面的代碼將幫助你我面臨同樣的問題。 我的任務是合並兩個 mp4 文件,當我從命令提示符執行此操作時,我成功合並。 但是當我通過asp做它時卡住了。 網。 我的問題通過給定的正確路徑解決並從命令中刪除 ffmpeg。 對於 E. G( ffmepg - f concat TO - f concat)

Dim _ffmpeg As String = "D:\\Develop\\Experiment\\mergermp4Vb\\mergermp4Vb\\bin\\ffmpeg.exe" Dim params = "-f concat -i D:\\Develop\\Experiment\\mergermp4Vb\\mergermp4Vb\\Videos\\mylist2.txt -c復制 D:\\Develop\\Experiment\\mergermp4Vb\\mergermp4Vb\\Videos\\0104.mp4" Dim _FFmpegProcessPropertys As New ProcessStartInfo _FFmpegProcessPropertys.FileName = _ffmpeg

_FFmpegProcessPropertys.Arguments = params
_FFmpegProcessPropertys.UseShellExecute = False
_FFmpegProcessPropertys.RedirectStandardOutput = True
_FFmpegProcessPropertys.RedirectStandardError = True
_FFmpegProcessPropertys.CreateNoWindow = True
Dim FFmpegProcess = Process.Start(_FFmpegProcessPropertys)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM