c# - 从 shell 运行的 ffmpeg 运行正常,但在 .NET 中调用时运行不正常

标签 c# .net ffmpeg cygwin

我正在尝试在 C# 程序中使用 ffmpeg(在 Windows 上使用 Cygwin 编译),方法是使用 Process 类生成一个 ffmpeg 实例。但是,我遇到了一个没有多大意义的相当奇怪的错误。

当我直接从 shell(无论是 Cygwin 的 bash、PowerShell、cmd)运行 ffmpeg 时,ffmpeg 可以正确地解码和重新编码文件而不会出现任何问题:

PS C:\audio> ffmpeg -i .\sound1.wav -acodec libvorbis -f ogg abc.ogg
ffmpeg version 1.2 Copyright (c) 2000-2013 the FFmpeg developers
  built on Apr  8 2013 15:10:40 with gcc 4.5.3 (GCC)
  configuration: --disable-encoder=vorbis --enable-libvorbis
  libavutil      52. 18.100 / 52. 18.100
  libavcodec     54. 92.100 / 54. 92.100
  libavformat    54. 63.104 / 54. 63.104
  libavdevice    54.  3.103 / 54.  3.103
  libavfilter     3. 42.103 /  3. 42.103
  libswscale      2.  2.100 /  2.  2.100
  libswresample   0. 17.102 /  0. 17.102
[wav @ 0x800538a0] max_analyze_duration 5000000 reached at 5015510 microseconds
Guessed Channel Layout for  Input Stream #0.0 : stereo
Input #0, wav, from '.\sound1.wav':
  Metadata:
    encoder         : Lavf54.63.104
  Duration: 00:00:05.76, bitrate: 1411 kb/s
    Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 44100 Hz, stereo, s16, 1411 kb/s
Output #0, ogg, to 'abc.ogg':
  Metadata:
    encoder         : Lavf54.63.104
    Stream #0:0: Audio: vorbis, 44100 Hz, stereo, fltp
Stream mapping:
  Stream #0:0 -> #0:0 (pcm_s16le -> libvorbis)
Press [q] to stop, [?] for help
size=      55kB time=00:00:05.74 bitrate=  78.5kbits/s
video:0kB audio:51kB subtitle:0 global headers:4kB muxing overhead 0.817473%

文件播放正常,我可以编码为 WAV 或我喜欢的任何其他格式。但是,当我使用以下代码从 C# 调用 ffmpeg 时:

string tempfile = Path.GetTempFileName();
FileStream tempfilestr = File.OpenWrite(tempfile);
input.CopyTo(tempfilestr);

ProcessStartInfo pstart = new ProcessStartInfo("ffmpeg", string.Format("-i \"{0}\" -v verbose -y -f wav -", tempfile));
pstart.CreateNoWindow = true;
pstart.ErrorDialog = false;
pstart.RedirectStandardOutput = true;
pstart.RedirectStandardError = true;
pstart.UseShellExecute = false;


Process proc = new Process();
proc.StartInfo = pstart;
proc.Start();
StreamReader stdout = proc.StandardOutput;
StreamReader stderr = proc.StandardError;

outtempfilestr = File.OpenRead(outtempfile);
MemoryStream output = new MemoryStream();

stdout.BaseStream.CopyTo(output);

try {
    proc.Kill();
}
catch(InvalidOperationException) { }
catch(Win32Exception) { }

File.Delete(tempfile);

return output.ToArray();

随机在输出中产生错误:

ffmpeg version 1.2 Copyright (c) 2000-2013 the FFmpeg developers
  built on Apr  8 2013 15:10:40 with gcc 4.5.3 (GCC)
  configuration: --disable-encoder=vorbis --enable-libvorbis
  libavutil      52. 18.100 / 52. 18.100
  libavcodec     54. 92.100 / 54. 92.100
  libavformat    54. 63.104 / 54. 63.104
  libavdevice    54.  3.103 / 54.  3.103
  libavfilter     3. 42.103 /  3. 42.103
  libswscale      2.  2.100 /  2.  2.100
  libswresample   0. 17.102 /  0. 17.102
[wav @ 0x80053860] parser not found for codec pcm_s16le, packets or times may be invalid.
    Last message repeated 1 times
[wav @ 0x80053860] max_analyze_duration 5000000 reached at 5015510 microseconds
Guessed Channel Layout for  Input Stream #0.0 : stereo
Input #0, wav, from 'C:\Users\Bevin\AppData\Local\Temp\tmp1CCE.tmp':
  Duration: 00:00:05.20, bitrate: 1411 kb/s
    Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 44100 Hz, stereo, s16, 1411 kb/s
[graph 0 input from stream 0:0 @ 0x8011f320] tb:1/44100 samplefmt:s16 samplerate:44100 chlayout:0x3
Output #0, wav, to 'pipe:':
  Metadata:
    ISFT            : Lavf54.63.104
    Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 44100 Hz, stereo, s16, 1411 kb/s
Stream mapping:
  Stream #0:0 -> #0:0 (pcm_s16le -> pcm_s16le)
Press [q] to stop, [?] for help
Multiple frames in a packet from stream 0
[pcm_s16le @ 0x8005c160] Invalid PCM packet, data has size 3 but at least a size of 4 was expected
Error while decoding stream #0:0: Invalid data found when processing input
No more output streams to write to, finishing.
size=     896kB time=00:00:05.20 bitrate=1411.3kbits/s    
video:0kB audio:896kB subtitle:0 global headers:0kB muxing overhead 0.008719%

请注意,这些错误并不总是会发生。有时它们会发生在某些文件上,有时则不会。我尝试了流重定向和临时文件的各种组合,但都不起作用。我还验证了临时文件的完整性,并且全部检查出来了。我什至在临时文件被删除之前提取了它,并在 shell 中顺利解码了它。

有什么想法吗?

编辑:我试过从通过 C# 运行的 shell 脚本运行 ffmpeg。它给出了相同的问题。通过 MinGW 编译 ffmpeg 也会出现同样的问题。

最佳答案

这是因为您传递参数的方式。这样做:

Process process = new Process();
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.FileName = Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName) +
                        @"\bin\ffmpeg.exe";

process.StartInfo.Arguments = "-i .\sound1.wav -acodec libvorbis -f ogg abc.ogg";


process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.Start();

这很完美。

关于c# - 从 shell 运行的 ffmpeg 运行正常,但在 .NET 中调用时运行不正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15922175/

相关文章:

c# - 访问 Windows 注册表 LOCAL_MACHINE 中的键

c# - bool 数组的值

c# - 如何为每个已安装的实例生成新的 guid?

c# - 为什么实例化一个对象不自动实例化它的属性?

c# - 在 Aspx.cs 中调用函数并更改下拉框

ffmpeg - 如何使用 FFmpeg hstack 过滤器合成 2 个视频?

c# - Asp.Net WebApi 模型绑定(bind)可为空的日期时间

c# - C#从PC到手机的SMS短信

user-interface - 转换为 f4v 的最便宜的 GUI

image - 如何使用 2 个图像 ffmpeg 创建图像