PHP exec() 和反斜杠\

标签 php ffmpeg exec

我正在尝试在 exec 上运行以下命令:

ffmpeg -y -i video.mp4 \
 -ss 1067 -i video.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts -t 32 tmp/cuts/6.ts \
 -ss 1215 -i video.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts -t 32 tmp/cuts/7.ts

如果我复制这个命令并在 shell 上运行它,一切都很好,视频被剪切,完美。

但是通过 php exec 运行,ffmpeg 返回以下错误:
[NULL @ 052a0060] Unable to find a suitable output format for '\'
\: Invalid argument

即使我复制粘贴这样的命令:
<?php
    $command = 'ffmpeg -y -i video.mp4 \
         -ss 1067 -i video.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts -t 32 tmp/cuts/6.ts \
         -ss 1215 -i video.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts -t 32 tmp/cuts/7.ts';

    echo $command.chr(10);

    $return = 0;
    $output = array();
    exec($command, $output, $return);

 ?>

我错过了什么吗?我试过escapeshellcmd , escapeshellarg ,甚至双反斜杠来逃避反斜杠,什么都没有。

这在 Windows 和 Unix 中都会发生,错误是完全相同的。

知道这是怎么回事吗?

最佳答案

在这种情况下,shell 脚本中的反斜杠 (\) 仅用于使其忽略换行符 ( Reference )

因此,请尝试在一行中运行不带反斜杠的命令:

<?php
    $command = 'ffmpeg -y -i video.mp4 -ss 1067 -i video.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts -t 32 tmp/cuts/6.ts -ss 1215 -i video.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts -t 32 tmp/cuts/7.ts';
    (...)
?>

关于PHP exec() 和反斜杠\,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29111014/

相关文章:

php - 将 Php 变量与 img src 中的字符串连接起来

Android:ExoPlayer - 从视频中获取当前帧数

bash - 如何在 docker 容器中运行/bin/bash?

java - 子任务在自己的堆空间

PHP 处理丢失的 XML 数据

PHP file_get_contents 返回错误

php - 如何通过 jquery 获取动态创建的 javascript div id 并存储在 php 中?

ffmpeg - drawbox 不能在远程服务器上工作,只能在本地工作

windows - ffmpeg只保存第一帧

c - 通过 C exec() 执行 IPtables