php - AJAX - PHP - FFMPEG - 执行未完成

标签 php ajax ffmpeg

这是一个稍微棘手的情况:

我正在使用 ajax 调用使用 FFMpeg 的函数转换视频。

代码一切正常,除了:

执行被缩短了,所以我最终得到了一个 2 帧的视频。

我认为这是导致问题的 ajax,因为就其而言,它调用了该函数并将成功输出返回到我的 php 页面。

换句话说,当 ajax 完成时,ffmpeg 脚本会中断。

有没有办法告诉 ajax 等待 ffmpeg 函数完成,或者我需要设置一个 cron 作业让它在后台运行?

编辑:

继承人的代码:

Ajax :

// watermark($thevideo)
var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";
var formData = new FormData();

formData.append('thevideo', thevideo);
formData.append('action', "watermark");

$.ajax({
url: ajaxurl,
type: "POST",
data:formData,cache: false,
processData: false, // Don't process the files
contentType: false, // Set content type to false as jQuery will tell the server its a query string request
success:function(data) {
    alert(data);
}

});

PHP FFMPEG 功能:
// Watermark ***************
// IMPORTANT!
// This action & function can be called by ajax but requires absolute file paths!

add_action( 'wp_ajax_watermark', 'do_watermark' );
add_action( 'wp_ajax_nopriv_watermark', 'do_watermark' );

function getabspath( $file_url ){
   return realpath($_SERVER['DOCUMENT_ROOT'] . parse_url( $file_url, PHP_URL_PATH ));
}

function do_watermark() {

    session_start();   
    ini_set('display_errors', 1);
    error_reporting(E_ALL);

//  $thevideo = $_POST['thevideo'];
    $thevideo = getabspath($_POST['thevideo']);

    $newvideo = getabspath('./wp-content/uploads') . '/test.mp4';

    $thewatermark = getabspath('./wp-content/uploads/mefbpic.png');
//  For some reason we have to OMIT THE DRIVE LETTER from the watermark image path
//  AND the backslashes need to be turned forward even tho its an absolute path!?!
    $thewatermark = substr($thewatermark,2); // Cuts off the first 2 chars. - (C:)
    $thewatermark = str_replace('\\','/',$thewatermark);

//  require_once('./vendor/autoload.php');
    require_once(getabspath('./vendor/autoload.php'));

$ffmpeg = FFMpeg\FFMpeg::create(array(
'ffmpeg.binaries' => getabspath('./FFMpeg/bin/ffmpeg.exe'),
'ffprobe.binaries' => getabspath('./FFMpeg/bin/ffprobe.exe'),
'timeout' => 3600, // The timeout for the underlying process
'ffmpeg.threads' => 12, // The number of threads that FFMpeg should use
));

    $video = $ffmpeg->open($thevideo);

    $video
      ->filters()
      ->resize(new \FFMpeg\Coordinate\Dimension(640, 360))
      ->watermark($thewatermark, [
        'position' => 'relative',
        'bottom' => 50,
        'right' => 50,
      ])
      ->synchronize();
    //$video
    //  ->save(new \FFMpeg\Format\Video\X264(), $thevideo);

        $format = new \FFMpeg\Format\Video\X264();
        $format->setAdditionalParameters(array('-y'));
        $video->save($format, $newvideo);

    echo 'done!';

}

最佳答案

解决它!

只需要添加

async: false,

到ajax请求!

干杯;)

关于php - AJAX - PHP - FFMPEG - 执行未完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52202510/

相关文章:

python - 在 Windows 上的 Python Popen 子进程中暂停 FFmpeg 编码

php/mysql变量

php - 如何维护各国时间戳

php - 未找到 Laravel 类 'HASH'

javascript - PHP 回显 jQuery AJAX 请求

javascript - 我收到消息 "resource interpreted as script but transferred with MIME type text/html"

performance - 用ffmpeg改变播放速度的正确方法是什么?

html - 使用 ffmpeg 从 avi 转换为 mp4

php - 如何使用 javascript 创建一个简单的按钮来调用 zend 框架中的 zend 操作?

php - 图片库,预加载与 AJAX