2016-03-04 152 views
0

我想用FFmpeg转换视频,一切都正常,但现在我试图在后台执行转换过程,所以我不必等待进程一分钟左右完成。Symfony后台进程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

检查了这一点:https://github.com/mac-cain13/daemonizable-command – Vardius

+0

http://symfony.com/doc/current/components/http_kernel/introduction.html #的内核 - 终止事件 – malcolm

回答

0

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

新的服务应该得到“上传”实体过去了ID(即在冲洗后产生)

那么你应该创建一个控制台命令,将获得该服务,并称之为“转换”公共法传入ID为PARAM。

的服务,让您拥有在控制器+更新“未转换”标志(OFC如果您需要跟踪这种状态)相同的ffmpeg的逻辑

你的控制器应在后台调用新的控制台命令,了解细节,可以看到我的建议在这个答案Asynchronously calling a Command in Symfony2