首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Symfony背景处理FFmpeg视频转换

Symfony背景处理FFmpeg视频转换
EN

Stack Overflow用户
提问于 2016-03-04 12:12:54
回答 1查看 1.6K关注 0票数 0

我正在尝试用FFmpeg转换一个视频,一切都很好,但是现在我尝试在后台完成转换过程,所以我不需要等待这个过程马上完成。

我该怎么做,有什么建议吗?

这是控制器中的当前代码:

代码语言:javascript
复制
$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 );
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-04 16:09:24

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

新服务应该通过传递id (在刷新后生成)获得“上载”实体。

然后,您应该创建一个控制台命令,该命令将获得该服务,并使用传递的id作为param调用“转换”公共方法。

该服务将产生与控制器+ update“未转换”标志(如果需要跟踪此状态)中相同的ffmpeg逻辑。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35795798

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档