我正在尝试使用FFmpeg转换视频,一切正常,但现在我尝试在后台进行转换过程,所以我不必等待过程完成在一分钟左右。
我有什么建议可以做到这一点吗?
这是我在控制器中的当前代码:
$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 );
}
答案 0 :(得分:0)
首先,您应该将ffmpeg逻辑从控制器移动到服务。 使用新的“未转换”标记清除“上传”。
新服务应该通过传递的id(在刷新后生成)获得“上传”实体
然后你应该创建一个控制台命令来获取该服务并调用“转换”公共方法,并将传递的id作为param。
该服务将使您在控制器中具有相同的ffmpeg逻辑+更新“未转换”标志(如果您需要跟踪此状态,则为ofc)
您的控制器应该在后台调用新的控制台命令,有关详细信息,您可以在此答案中看到我的建议Asynchronously calling a Command in Symfony2