通过c#.net执行时ffmpeg无法转换上传大小大于14mb的视频文件

发布于 2024-10-02 15:24:34 字数 4190 浏览 3 评论 0原文

我正在通过 c# 使用 ffmpeg.exe 将视频转换为 flv如果文件大小超过 14mb,进程就会卡住并且转换失败。我尝试直接通过命令提示符运行ffmpeg,无论大小如何,它都可以正常工作

我的转换代码如下:

OutputFile = SavePath + "SWF\\" + WithOutExt + ".flv";
FilArgs = string.Format("-i {0} -ar 22050 -qscale 1 {1}",InputFile,OutputFile);

Process proc;
proc = new Process();

try
{
     proc.StartInfo.FileName = spath + "\\ffmpeg\\ffmpeg.exe";
     proc.StartInfo.Arguments = FilArgs;
     proc.StartInfo.UseShellExecute = false;
     proc.StartInfo.CreateNoWindow = false;
     proc.StartInfo.RedirectStandardOutput = true;
     proc.StartInfo.RedirectStandardError = true;
     proc.Start();

     string StdOutVideo = proc.StandardOutput.ReadToEnd();
     string StdErrVideo = proc.StandardError.ReadToEnd();
 }
 finally
 {
     proc.WaitForExit();
     proc.Close();
 }

当我尝试手动停止正在运行的进程时 stderroutput 如下:

FFmpeg 版本 SVN-r21231-Sherpya,版权所有 (c) 2000-2010 Fabrice Bellard, et al. 建于 Jan 16 2010 05:42:31 with gcc 4.2.5 20080919 (prerelease) [Sherpya] libavutil 50.7.0 / 50.7.0 libavcodec 52.47。 0 / 52.47。 0 libav 格式 52.47。 0 / 52.47。 0 libavdevice 52.2.0 / 52.2.0 libavfilter 1.15。 0 / 1.15。 0 libswscale 0.8.0 / 0.8.0 libpostproc 51.2.0 / 51.2.0 输入 #0, mov,mp4,m4a,3gp,3g2,mj2,来自 'C:\Project-Folder-Files\ThirtySecondPitch\video\7.mp_c88a8ec2-3a60-4133-aee6-7e2d93b62324.mp4': 元数据: 主要品牌:isom 次要版本:512 兼容品牌:isomiso2mp41 持续时间:00:00:23.13,开始:0.000000,比特率:9261 kb/s 流 #0.0(jpn):视频:mpeg4、yuv420p、1280x720 [PAR 1:1 DAR 16:9]、9128 kb/s、30 tbr、30 tbn、30 tbc 流 #0.1(jpn):音频:aac、48000 Hz、立体声、s16、127 kb/s 将 #0, flv 输出到“C:\Project-Folder-Files\ThirtySecondPitch\video\SWF\7.mp_c88a8ec2-3a60-4133-aee6-7e2d93b62324.flv”: 流 #0.0(jpn):视频:flv、yuv420p、1280x720 [PAR 1:1 DAR 16:9]、q=2-31、200 kb/s、1k tbn、30 tbc 流 #0.1(jpn):音频:libmp3lame、22050 Hz、立体声、s16、64 kb/s 流映射: 流 #0.0 -> #0.0 流 #0.1 -> #0.1 按[q]停止编码 帧= 19 fps= 0 q=1.0 大小= 596kB 时间=0.50 比特率=9839.2kbits/s
帧= 36 fps= 33 q=1.0 大小= 1284kB 时间=1.07 比特率=9822.2kbits/s
帧= 55 fps= 34 q=1.0 大小= 2076kB 时间=1.70 比特率=10013.6kbits/s
帧= 74 fps= 35 q=1.0 大小= 2819kB 时间=2.35 比特率=9822.2kbits/s
帧= 93 fps= 35 q=1.0 大小= 3583kB 时间=2.95 比特率=9943.3kbits/s
帧= 113 fps= 36 q=1.0 大小= 4457kB 时间=3.63 比特率=10056.2kbits/s
帧= 132 fps= 36 q=1.0 大小= 5157kB 时间=4.26 比特率=9922.3kbits/s
帧= 152 fps= 37 q=1.0 大小= 5972kB 时间=4.91 比特率=9961.6kbits/s
帧= 172 fps= 37 q=1.0 大小= 6817kB 时间=5.62 比特率=9943.9kbits/s
帧= 192 fps= 37 q=1.0 大小= 7676kB 时间=6.27 比特率=10030.1kbits/s
帧= 212 fps= 37 q=1.0 大小= 8488kB 时间=6.92 比特率=10045.9kbits/s
帧= 232 fps= 37 q=1.0 大小= 9306kB 时间=7.58 比特率=10062.8kbits/s
帧= 251 fps= 37 q=1.0 大小= 10150kB 时间=8.26 比特率=10072.1kbits/s
帧= 271 fps= 37 q=1.0 大小= 10970kB 时间=8.88 比特率=10117.6kbits/s
帧= 291 fps= 38 q=1.0 大小= 11815kB 时间=9.56 比特率=10123.7kbits/s
帧= 311 fps= 38 q=1.0 大小= 12625kB 时间=10.24 比特率=10100.1kbits/s
帧= 331 fps= 38 q=1.0 大小= 13414kB 时间=10.89 比特率=10088.0kbits/s
帧= 351 fps= 38 q=1.0 大小= 14125kB 时间=11.55 比特率=10021.7kbits/s
帧= 371 fps= 38 q=1.0 大小= 14929kB 时间=12.25 比特率=9983.0kbits/s
帧= 391 fps= 38 q=1.0 大小= 15701kB 时间=12.90 比特率=9967.8kbits/s
帧= 411 fps= 38 q=1.0 大小= 16385kB 时间=13.56 比特率=9899.9kbits/s
帧= 431 fps= 38 q=1.0 大小= 17137kB 时间=14.21 比特率=9878.7kbits/s
帧= 451 fps= 38 q=1.0 大小= 17938kB 时间=14.92 比特率=9851.7kbits/s
帧= 471 fps= 38 q=1.0 大小= 18663kB 时间=15.57 比特率=9820.0kbits/s
帧= 491 fps= 38 q=1.0 大小= 19379kB 时间=16.22 比特率=9786.4kbits/s
帧= 511 fps= 38 q=1.0 大小= 20179kB 时间=16.90 比特率=9780.9kbits/s
帧= 530 fps= 38 q=1.0 大小= 20893kB 时间=17.53 比特率=9764.8kbits/s
帧= 550 fps= 38 q=1.0 大小= 21633kB 时间=18.18 比特率=9747.3kbits/s
帧= 570 fps= 38 q=1.0 大小= 22464kB 时间=18.89 比特率=9743.5kbits/s
帧= 590 fps= 38 q=1.0 大小= 23227kB 时间=19.54 比特率=9737.7kbits/s
帧= 609 fps= 38 q=1.0 大小= 23992kB 时间=20.17 比特率=9745.8kbits/s
帧= 629 fps= 38 q=1.0 大小= 24804kB 时间=20.85 比特率=9747.4kbits/s
帧= 648 fps= 38 q=1.0 大小= 25501kB 时间=21.45 比特率=9740.6kbits/s
帧= 668 fps= 38 q=1.0 大小= 26306kB 时间=22.13 比特率=9739.7kbits/s
帧= 687 fps= 38 q=1.0 大小= 27040kB 时间=22.80 比特率=9713.2kbits/s

I am converting videos to flv using ffmpeg.exe through c# and the process gets stuck and the conversion fails if the file size exceeds 14mb. I've tried to run ffmpeg directly through command prompt and it works fine regardless of size.

My conversion code is as follows:

OutputFile = SavePath + "SWF\\" + WithOutExt + ".flv";
FilArgs = string.Format("-i {0} -ar 22050 -qscale 1 {1}",InputFile,OutputFile);

Process proc;
proc = new Process();

try
{
     proc.StartInfo.FileName = spath + "\\ffmpeg\\ffmpeg.exe";
     proc.StartInfo.Arguments = FilArgs;
     proc.StartInfo.UseShellExecute = false;
     proc.StartInfo.CreateNoWindow = false;
     proc.StartInfo.RedirectStandardOutput = true;
     proc.StartInfo.RedirectStandardError = true;
     proc.Start();

     string StdOutVideo = proc.StandardOutput.ReadToEnd();
     string StdErrVideo = proc.StandardError.ReadToEnd();
 }
 finally
 {
     proc.WaitForExit();
     proc.Close();
 }

When I tried to manually stop the running process The stderroutput is as follows:

FFmpeg version SVN-r21231-Sherpya, Copyright (c) 2000-2010 Fabrice Bellard, et al.
built on Jan 16 2010 05:42:31 with gcc 4.2.5 20080919 (prerelease) [Sherpya]
libavutil 50. 7. 0 / 50. 7. 0
libavcodec 52.47. 0 / 52.47. 0
libavformat 52.47. 0 / 52.47. 0
libavdevice 52. 2. 0 / 52. 2. 0
libavfilter 1.15. 0 / 1.15. 0
libswscale 0. 8. 0 / 0. 8. 0
libpostproc 51. 2. 0 / 51. 2. 0
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'C:\Project-Folder-Files\ThirtySecondPitch\video\7.mp_c88a8ec2-3a60-4133-aee6-7e2d93b62324.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2mp41
Duration: 00:00:23.13, start: 0.000000, bitrate: 9261 kb/s
Stream #0.0(jpn): Video: mpeg4, yuv420p, 1280x720 [PAR 1:1 DAR 16:9], 9128 kb/s, 30 tbr, 30 tbn, 30 tbc
Stream #0.1(jpn): Audio: aac, 48000 Hz, stereo, s16, 127 kb/s
Output #0, flv, to 'C:\Project-Folder-Files\ThirtySecondPitch\video\SWF\7.mp_c88a8ec2-3a60-4133-aee6-7e2d93b62324.flv':
Stream #0.0(jpn): Video: flv, yuv420p, 1280x720 [PAR 1:1 DAR 16:9], q=2-31, 200 kb/s, 1k tbn, 30 tbc
Stream #0.1(jpn): Audio: libmp3lame, 22050 Hz, stereo, s16, 64 kb/s
Stream mapping:
Stream #0.0 -> #0.0
Stream #0.1 -> #0.1
Press [q] to stop encoding
frame= 19 fps= 0 q=1.0 size= 596kB time=0.50 bitrate=9839.2kbits/s
frame= 36 fps= 33 q=1.0 size= 1284kB time=1.07 bitrate=9822.2kbits/s
frame= 55 fps= 34 q=1.0 size= 2076kB time=1.70 bitrate=10013.6kbits/s
frame= 74 fps= 35 q=1.0 size= 2819kB time=2.35 bitrate=9822.2kbits/s
frame= 93 fps= 35 q=1.0 size= 3583kB time=2.95 bitrate=9943.3kbits/s
frame= 113 fps= 36 q=1.0 size= 4457kB time=3.63 bitrate=10056.2kbits/s
frame= 132 fps= 36 q=1.0 size= 5157kB time=4.26 bitrate=9922.3kbits/s
frame= 152 fps= 37 q=1.0 size= 5972kB time=4.91 bitrate=9961.6kbits/s
frame= 172 fps= 37 q=1.0 size= 6817kB time=5.62 bitrate=9943.9kbits/s
frame= 192 fps= 37 q=1.0 size= 7676kB time=6.27 bitrate=10030.1kbits/s
frame= 212 fps= 37 q=1.0 size= 8488kB time=6.92 bitrate=10045.9kbits/s
frame= 232 fps= 37 q=1.0 size= 9306kB time=7.58 bitrate=10062.8kbits/s
frame= 251 fps= 37 q=1.0 size= 10150kB time=8.26 bitrate=10072.1kbits/s
frame= 271 fps= 37 q=1.0 size= 10970kB time=8.88 bitrate=10117.6kbits/s
frame= 291 fps= 38 q=1.0 size= 11815kB time=9.56 bitrate=10123.7kbits/s
frame= 311 fps= 38 q=1.0 size= 12625kB time=10.24 bitrate=10100.1kbits/s
frame= 331 fps= 38 q=1.0 size= 13414kB time=10.89 bitrate=10088.0kbits/s
frame= 351 fps= 38 q=1.0 size= 14125kB time=11.55 bitrate=10021.7kbits/s
frame= 371 fps= 38 q=1.0 size= 14929kB time=12.25 bitrate=9983.0kbits/s
frame= 391 fps= 38 q=1.0 size= 15701kB time=12.90 bitrate=9967.8kbits/s
frame= 411 fps= 38 q=1.0 size= 16385kB time=13.56 bitrate=9899.9kbits/s
frame= 431 fps= 38 q=1.0 size= 17137kB time=14.21 bitrate=9878.7kbits/s
frame= 451 fps= 38 q=1.0 size= 17938kB time=14.92 bitrate=9851.7kbits/s
frame= 471 fps= 38 q=1.0 size= 18663kB time=15.57 bitrate=9820.0kbits/s
frame= 491 fps= 38 q=1.0 size= 19379kB time=16.22 bitrate=9786.4kbits/s
frame= 511 fps= 38 q=1.0 size= 20179kB time=16.90 bitrate=9780.9kbits/s
frame= 530 fps= 38 q=1.0 size= 20893kB time=17.53 bitrate=9764.8kbits/s
frame= 550 fps= 38 q=1.0 size= 21633kB time=18.18 bitrate=9747.3kbits/s
frame= 570 fps= 38 q=1.0 size= 22464kB time=18.89 bitrate=9743.5kbits/s
frame= 590 fps= 38 q=1.0 size= 23227kB time=19.54 bitrate=9737.7kbits/s
frame= 609 fps= 38 q=1.0 size= 23992kB time=20.17 bitrate=9745.8kbits/s
frame= 629 fps= 38 q=1.0 size= 24804kB time=20.85 bitrate=9747.4kbits/s
frame= 648 fps= 38 q=1.0 size= 25501kB time=21.45 bitrate=9740.6kbits/s
frame= 668 fps= 38 q=1.0 size= 26306kB time=22.13 bitrate=9739.7kbits/s
frame= 687 fps= 38 q=1.0 size= 27040kB time=22.80 bitrate=9713.2kbits/s

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

八巷 2024-10-09 15:24:34

问题不在于正在转换的文件的大小。

您正在尝试对两个重定向流的末尾执行两次同步读取。这可能会导致死锁(我的猜测:你已经发现它是因为文件大小较大)。

因此,您应该注释这些行之一:

string StdOutVideo = proc.StandardOutput.ReadToEnd();
string StdErrVideo = proc.StandardError.ReadToEnd();

或从其中一个流异步读取。

仅供参考: ffmpeg 使用 stderr 进行日志记录(假设您想要捕获输出);并使用 stdout 输出二进制数据(多媒体、快照等)。所以我没有看到标准输出重定向的真正原因。

The problem is not in the size of file being converted.

You're trying to perform two synchronous read to the end of both redirected streams. This may cause a deadlock (my guess: you've caught it because of lager file size).

So you should to comment one of these lines:

string StdOutVideo = proc.StandardOutput.ReadToEnd();
string StdErrVideo = proc.StandardError.ReadToEnd();

or read from one of the streams asynchronously.

FYI: ffmpeg uses stderr for logging purposes (suppose, you want to capture the output); and uses stdout to pipe out binary data(multimedia, snapshots, etc.). So I don't see a real reason for stdout redirection.

回忆那么伤 2024-10-09 15:24:34

我认为如果您使用的是 asp.net,该过程一定会提前终止。我遇到了这个问题,我所做的解决方案是执行一个在服务器上运行的服务,该服务轮询新文件并进行转换。效果很好,但您至少需要一个虚拟专用服务器,因为大多数共享主机不允许您的服务运行。

I think the process must be terminating prematurely, if you're using asp.net. I had this problem, and what I did for a solution I did was to do a service that ran on the server, which polls for new files and does the conversion. Worked well, but you need at least a Virtual Dedicated server, since most shared hosts will not allow your service to run..

青春如此纠结 2024-10-09 15:24:34

希望这段代码对您有帮助,请将这段代码转换为 C# 形式的 vb。

Function convertotswf(ByVal imgName As String, ByVal iMaxId As Integer) As String
    Dim filename As String
    Dim _mhandler As New MediaHandler()

    Dim RootPath As String = System.Configuration.ConfigurationManager.AppSettings("SITE_ROOTPATH")

    _mhandler.FFMPEGPath = RootPath + System.Configuration.ConfigurationManager.AppSettings("ffmpegpath")
    _mhandler.InputPath = RootPath + System.Configuration.ConfigurationManager.AppSettings("TEMPVIDEO_ROOTPATH")
    _mhandler.OutputPath = RootPath + System.Configuration.ConfigurationManager.AppSettings("mediapath")
    _mhandler.FileName = imgName
    _mhandler.OutputFileName = iMaxId & ".flv"
    filename = iMaxId & ".flv"

    _mhandler.Video_Bitrate = System.Configuration.ConfigurationManager.AppSettings("flvvideobitrate")
    _mhandler.Audio_Bitrate = System.Configuration.ConfigurationManager.AppSettings("flvaudiobitrate")
    _mhandler.Audio_SamplingRate = System.Configuration.ConfigurationManager.AppSettings("flvsamplingrate")

    Dim info As VideoInfo = _mhandler.Encode_FLV()
    If info.ErrorCode > 0 Then
        If info.ErrorCode = 121 Then
            GoTo read
        End If
        Return "1"

    Else
    read:
        Dim mid_duration As String = System.Configuration.ConfigurationManager.AppSettings("flvmidduration")

        _mhandler.FFMPEGPath = RootPath & System.Configuration.ConfigurationManager.AppSettings("ffmpegpath")
        _mhandler.InputPath = RootPath + System.Configuration.ConfigurationManager.AppSettings("mediapath")
        _mhandler.OutputPath = RootPath + System.Configuration.ConfigurationManager.AppSettings("flvimagepath")
        _mhandler.FileName = filename
        _mhandler.Image_Format = "jpg"
        _mhandler.ImageName = iMaxId & ".jpg"
        _mhandler.Frame_Time = mid_duration
        _mhandler.Width = System.Configuration.ConfigurationManager.AppSettings("flvwidth")
        _mhandler.Height = System.Configuration.ConfigurationManager.AppSettings("flvheight")
        _mhandler.Grab_Thumb()
    End If

    Return filename
End Function

将代码上传到服务器后使用它,

Dim RootPath As String = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath)

Hope this code is helpful to you, please convert this code to C# form vb.

Function convertotswf(ByVal imgName As String, ByVal iMaxId As Integer) As String
    Dim filename As String
    Dim _mhandler As New MediaHandler()

    Dim RootPath As String = System.Configuration.ConfigurationManager.AppSettings("SITE_ROOTPATH")

    _mhandler.FFMPEGPath = RootPath + System.Configuration.ConfigurationManager.AppSettings("ffmpegpath")
    _mhandler.InputPath = RootPath + System.Configuration.ConfigurationManager.AppSettings("TEMPVIDEO_ROOTPATH")
    _mhandler.OutputPath = RootPath + System.Configuration.ConfigurationManager.AppSettings("mediapath")
    _mhandler.FileName = imgName
    _mhandler.OutputFileName = iMaxId & ".flv"
    filename = iMaxId & ".flv"

    _mhandler.Video_Bitrate = System.Configuration.ConfigurationManager.AppSettings("flvvideobitrate")
    _mhandler.Audio_Bitrate = System.Configuration.ConfigurationManager.AppSettings("flvaudiobitrate")
    _mhandler.Audio_SamplingRate = System.Configuration.ConfigurationManager.AppSettings("flvsamplingrate")

    Dim info As VideoInfo = _mhandler.Encode_FLV()
    If info.ErrorCode > 0 Then
        If info.ErrorCode = 121 Then
            GoTo read
        End If
        Return "1"

    Else
    read:
        Dim mid_duration As String = System.Configuration.ConfigurationManager.AppSettings("flvmidduration")

        _mhandler.FFMPEGPath = RootPath & System.Configuration.ConfigurationManager.AppSettings("ffmpegpath")
        _mhandler.InputPath = RootPath + System.Configuration.ConfigurationManager.AppSettings("mediapath")
        _mhandler.OutputPath = RootPath + System.Configuration.ConfigurationManager.AppSettings("flvimagepath")
        _mhandler.FileName = filename
        _mhandler.Image_Format = "jpg"
        _mhandler.ImageName = iMaxId & ".jpg"
        _mhandler.Frame_Time = mid_duration
        _mhandler.Width = System.Configuration.ConfigurationManager.AppSettings("flvwidth")
        _mhandler.Height = System.Configuration.ConfigurationManager.AppSettings("flvheight")
        _mhandler.Grab_Thumb()
    End If

    Return filename
End Function

after uploading the code to the server use this,

Dim RootPath As String = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath)
娇纵 2024-10-09 15:24:34

正如 Alex 所说,问题可能出在 ReadToEnd() 命令上,解决该问题的一种方法是创建一个事件处理程序,以便在出现输出或错误行时打印一行。这意味着您不会将它们全部添加到缓冲区中并(可能)使进程挂起。以下代码对我有用,只需将 log.Info() 更改为您想要对输出和错误执行的任何操作

process.OutputDataReceived += new DataReceivedEventHandler(
    (s, e) => 
    { 
        log.Info("Output Line: "+e.Data);
    }
);
process.ErrorDataReceived += new DataReceivedEventHandler(
    (s, e) => 
    { 
        log.Info("Error Line: "+e.Data);
    }
);
//start process
process.Start();
log.Info("process started");
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();

As Alex said, the problem is likely the ReadToEnd() commands, one way to fix it is to create an event handler to print a line whenever an output or error line occurs. This will mean you don't add them all to a buffer and (potentially) make the process hang. The following code worked for me, just change log.Info() to whatever you want to do with your output and error

process.OutputDataReceived += new DataReceivedEventHandler(
    (s, e) => 
    { 
        log.Info("Output Line: "+e.Data);
    }
);
process.ErrorDataReceived += new DataReceivedEventHandler(
    (s, e) => 
    { 
        log.Info("Error Line: "+e.Data);
    }
);
//start process
process.Start();
log.Info("process started");
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文