AJAX - PHP - FFMPEG - 执行未完成

问题描述 投票:0回答:1

这是一个有点棘手的情况:

我使用ajax调用一个使用FFMpeg转换视频的函数。

代码都工作正常除了:

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

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

换句话说,ffmpeg脚本在ajax完成时切断。

有没有办法告诉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!';

}
php ajax ffmpeg
1个回答
0
投票

固定它!

只需添加

async: false,

到ajax请求!

干杯;)

© www.soinside.com 2019 - 2024. All rights reserved.