php - Symfony 后台进程 FFmpeg 视频转换

标签 php symfony ffmpeg symfony-process

我正在尝试使用 FFmpeg 转换视频并且一切正常,但现在我正在尝试在后台进行转换过程,因此我不必等待该过程在一分钟左右完成。

关于如何做到这一点有什么建议吗?

这是我在 Controller 中的当前代码:

$urlGenerator = new urlGenerator();
$user = $this->getUser ();
$userid = $user->getid ();
$username = $user->getFullname ();
$emailcomment = $this->container->getParameter ( 'mailer_sender_address' );

$ffmpeg = $this->get ( 'dubture_ffmpeg.ffmpeg' );
$ffprobe = $this->get ( 'dubture_ffmpeg.ffprobe' );

$currentDateTime = date ( 'YmdHis' );
$upload = new UploadVideo();

$em = $this->getDoctrine ()->getManager ();
$form = $this->createForm ( new UploadVideoType(), $upload );
$userids = $em->getReference ( 'HotelPlanBundle\Entity\User', $userid );
$form->handleRequest ( $request );

if ( $form->isValid () || TRUE ) {
    $upload->upload ($userid);

    // Start transcoding and save video
    $upload->setUserid ( $userids );
    $upload->setTitle ( 'No title' );
    $upload->setCreatedDate ( new \DateTime() );

    $duration = $ffprobe
        ->format ( $upload->getWebPath () )// extracts file informations
        ->get ( 'duration' );

    $video = $ffmpeg->open ( $upload->getWebPath () );
    $video
        ->frame ( TimeCode::fromSeconds ( 10 ) )
        ->save ( __DIR__ . '/../../../web/uploads/documents/' . $currentDateTime . '.png' );

    $upload->setThumbnail ( $currentDateTime . '.png' );

    //here is where the converting takes too long !!
    $video->save ( new X264(), __DIR__ . '/../../../web/uploads/documents/' . $currentDateTime . '.mp4' );

    $upload->setVideoPath ( $currentDateTime . '.mp4' );
    $upload->setLink ( $urlGenerator->generateUrl () );
    $upload->setResetLink ( $urlGenerator->generateUrl () );

    try {
        $em->persist ( $upload );
        $em->flush ();
    } catch ( \Exception $e ) {
        if ( $e->getPrevious ()->getCode () == '23000' ) {
            $upload->setLink ( $urlGenerator->generateUrl () );
            $em->persist ( $upload );
            $em->flush ();
    }
}
$em->persist ( $upload );
$em->flush ();

return new JsonResponse( array (
    'message' => 'Sucessfully Uploaded',
    'last_id' => $upload->getId ()
), 200 );
}

最佳答案

首先,您应该将 ffmpeg 逻辑从 Controller 移动到服务。
用新的“未转换”标志刷新您的“上传”。

新服务应通过传递的 id 获取“上传”实体(在刷新后生成)

然后,您应该创建一个控制台命令,该命令将获取该服务并使用传递的 id 作为参数调用“convert”公共(public)方法。

该服务将生成与 Controller 中相同的 ffmpeg 逻辑 + 更新“未转换”标志(如果您需要跟踪此状态,则为 ofc)

您的 Controller 应该在后台调用新的控制台命令,有关详细信息,您可以在此答案中查看我的建议 Asynchronously calling a Command in Symfony2

关于php - Symfony 后台进程 FFmpeg 视频转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35795798/

相关文章:

c# - System.Diagnostics.Process 程序继续运行

ios - cvCreateFileCapture 总是返回 "NULL"

batch-file - 如何使用 FFmpeg 进行时间转换

javascript - 我应该通过 PHP 还是 JavaScript 进行哈希处理?

php - 当我尝试在 symfony 4 中哈希我的密码时,出现这个错误是什么?

symfony - 将 sonata_media_type 用作 1 :N via a sonata_type_collection field 时出现 500 错误

php - Symfony Doctrine 事件订阅者未在调试 :event-dispatcher 中列出

php - 我如何改进此 mysql 查询以提高性能

php - 根据mysql中的不同条件选择两列

php - Redis Cache() 不会接受 Predis/Client() 作为 Redis 的实例