Ejemplo n.º 1
0
 /**
  * Encode video
  *
  * @fixme проверить не осталось ли еще активных процессов на конвертацию от прошлой cron задачи, если осталось, то менять $threads для тещушего cron.
  * @return int
  * @throws \Exception
  * @throws \yii\web\UnsupportedMediaTypeHttpException
  */
 public function actionEncode()
 {
     /** @var Video $video */
     // get params
     $params = Yii::$app->params;
     $outputFormat = $params['video.encode.outputFormat'];
     $threads = $params['video.encode.threads'];
     $timeout = $params['video.encode.timeout'];
     $savePath = $params['path.video.converted'];
     // ffmpeg init
     $ffmpegConfig = ['ffmpeg.threads' => $threads, 'timeout' => $timeout];
     // video processing
     $error = null;
     $videos = Video::getUnconvertedVideo($threads);
     foreach ($videos as $video) {
         $video->converted_filename = FileHelper::generateFileName($outputFormat, $savePath);
         $filePaths = $video->getPaths();
         // video encode
         if (FfmpegHelper::convert($filePaths['upload'], $filePaths['converted'], $outputFormat, $ffmpegConfig)) {
             $meta = FfmpegHelper::getProperties($filePaths['converted']);
             $video->converted_video_bitrate = $meta['video_bitrate'];
             $video->converted_audio_bitrate = $meta['audio_bitrate'];
             $video->status = Video::STATUS_ENCODE_SUCCESS;
         } else {
             $video->status = Video::STATUS_ENCODE_ERROR;
             $error = true;
         }
         $video->save(false);
     }
     return $error === null ? Controller::EXIT_CODE_NORMAL : Controller::EXIT_CODE_ERROR;
 }
Ejemplo n.º 2
0
 /**
  * Upload and get properties from video file
  *
  * @return Video
  * @throws ForbiddenHttpException
  * @throws ServerErrorHttpException
  */
 public function actionCreate()
 {
     $this->checkAccess($this->action->id);
     $videoModel = new Video();
     $videoModel->file = UploadedFile::getInstanceByName('file');
     if ($videoModel->validate()) {
         // upload file
         $videoModel->upload_filename = FileHelper::generateFileName($videoModel->file->extension, Yii::$app->params['path.video.upload']);
         $filePaths = $videoModel->getPaths();
         $videoModel->file->saveAs($filePaths['upload'], false);
         // get file properties
         $meta = FfmpegHelper::getProperties($filePaths['upload']);
         if ($meta) {
             $videoModel->attributes = ['screen_width' => $meta['screen_width'], 'screen_height' => $meta['screen_height'], 'upload_video_bitrate' => $meta['video_bitrate'], 'upload_audio_bitrate' => $meta['audio_bitrate']];
         }
         // save model and create response
         if ($videoModel->save(false)) {
             $response = Yii::$app->getResponse();
             $response->setStatusCode(201);
             $response->getHeaders()->set('Location', Url::toRoute(['view', 'id' => $videoModel->id], true));
         }
     }
     return $videoModel;
 }