php - 通过php从管道发送图像时ffmpeg出错

标签 php ffmpeg pipe

使用以下教程通过管道将上传的图像从 php 发送到 ffmpeg。

Image pipe from php to ffmpeg

但是它会引发一个错误,指出管道中包含无效数据。

pipe:: Invalid data found when processing input

以下是上传图片文件的php代码
$tempPath = $_FILES['vdofile']['tmp_name'];

print_r($tempPath);

$descriptorspec = array(
    0 => array("pipe", "r"), // stdin is a pipe that the child will read from
    1 => array("pipe", "w"), // stdout is a pipe that the child will write to
    2 => array("file", "error-output.txt", "a") // stderr is a file to write to
    );

$img = imagecreatefromjpeg($tempPath);

 $proc = proc_open("ffmpeg image2pipe -i - -y image.jpg", $descriptorspec, $pipes);

 if (is_resource($proc)) {     
     /*
      * $pipes[0] = write something to command being run
      * $pipes[1] = read something from command
      * $pipes[2] = error from command 
      */
       fwrite($pipes[0], $_FILES['vdofile']['tmp_name']);
       fclose($pipes[0]);
 }
 proc_close($proc);
?>

简单的html上传代码如下:
 <form action="upload.php" method="post" enctype="multipart/form-data">
        <input type="file" name="vdofile" />

        <input type="submit" value="Upload" />
    </form>

生成的错误文件如下:
    ffmpeg version N-58485-ga12b4bd Copyright (c) 2000-2013 the FFmpeg developers
  built on Nov 26 2013 22:07:02 with gcc 4.8.2 (GCC)
  configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-avisynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libcaca --enable-libfreetype --enable-libgsm --enable-libilbc --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libx264 --enable-libxavs --enable-libxvid --enable-zlib
  libavutil      52. 55.100 / 52. 55.100
  libavcodec     55. 44.100 / 55. 44.100
  libavformat    55. 21.102 / 55. 21.102
  libavdevice    55.  5.101 / 55.  5.101
  libavfilter     3. 91.100 /  3. 91.100
  libswscale      2.  5.101 /  2.  5.101
  libswresample   0. 17.104 /  0. 17.104
  libpostproc    52.  3.100 / 52.  3.100
pipe:: Invalid data found when processing input

任何帮助将不胜感激。

**

编辑

**

代码仍然产生错误:
    ffmpeg version N-58485-ga12b4bd Copyright (c) 2000-2013 the FFmpeg developers
  built on Nov 26 2013 22:07:02 with gcc 4.8.2 (GCC)
  configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-avisynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libcaca --enable-libfreetype --enable-libgsm --enable-libilbc --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libx264 --enable-libxavs --enable-libxvid --enable-zlib
  libavutil      52. 55.100 / 52. 55.100
  libavcodec     55. 44.100 / 55. 44.100
  libavformat    55. 21.102 / 55. 21.102
  libavdevice    55.  5.101 / 55.  5.101
  libavfilter     3. 91.100 /  3. 91.100
  libswscale      2.  5.101 /  2.  5.101
  libswresample   0. 17.104 /  0. 17.104
  libpostproc    52.  3.100 / 52.  3.100
[image2pipe @ 00000000029ee0a0] Could not find codec parameters for stream 0 (Video: none): unspecified size
Consider increasing the value for the 'analyzeduration' and 'probesize' options
pipe:: could not find codec parameters

从 php 中调用以下 ffmpeg 命令,如上:
ffmpeg -f image2pipe -i - -vcodec mjpeg -vframes 1 -an -f mjpeg image.mp4

最佳答案

你有一个错字,你需要告诉 ffmpeg 管道图像是什么格式:

改变:

ffmpeg image2pipe -i - -y image.jpg

至:
ffmpeg -y -f image2pipe -codec:v mjpeg -i - image.jpg

此示例将您的输入管道图像重新编码为 image.jpg .
ffmpeg -y -f image2pipe -codec:v mjpeg -i - -codec copy image.jpg

这将只是 stream copy (重新复用)而不是重新编码。将其视为将管道图像复制并粘贴到输出。

您可能仍会收到 pipe:: Input/output error但是您可能会忽略它,因为流复制的图像与我的测试中的输入具有相同的 md5 哈希值。

我认为没有理由将单个图像传输到 ffmpeg 重新编码,但我看到您的代码只是用于测试管道。

关于php - 通过php从管道发送图像时ffmpeg出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20491385/

相关文章:

java 似乎没有及时读取我的 perl 管道输出并且正在填满

python - 在不破坏管道的情况下与进程多次通信?

php - SQL-如果不为空,则选择不同表中的列

javascript - 验证 php 和 javascript 中一个字段的值大于另一个字段的值

python - ffmpeg-python 过滤器描述中的流说明符 '' [0]concat=n=1[s0] 不匹配任何流

c - C 中 close(fd) 的反义词是什么?

php - 使用 preg_replace 时我得到双字符...?

php - CodeIgniter错误,显示核心文件路径而不是工作文件路径

ios - 大全景到平移视频

php - 所有设备的 FFmpeg 命令配置文件