2016-03-04 107 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