4

I have a form that takes video file as an input. It looks like this.

<?php $form = ActiveForm::begin(['options'=>['enctype'=>'multipart/form-data']]); ?>

<?= $form->field($model, 'file')->fileInput(['maxlength' => true]) ?> 

I want to get the duration, dimensions and size of the video. There is no mention about video parsing in the yiiframework documents. Is there a way to do this?

Edit : As suggested there is getID3() for native php to work with. Is there a way to do it in Yii2 without third party libraries? If not, how do I integrate getID3() into Yii2?

Red Bottle
  • 2,839
  • 4
  • 22
  • 59

3 Answers3

2

Add "james-heinrich/getid3": "*" to require section in project-directory/composer.json file, then in project directory (if you have well installed composer) run command:

composer update

Then in project use it like:

$getID3 = new \getID3;

$file = $getID3->analyze($pathToFIle);

Full library will be available in your project without unnecessary imports/requires.

Yupik
  • 4,932
  • 1
  • 12
  • 26
1

Solved.

Step 1: (Install FFmpeg from here)

Step 2: To get duration of video

public static function getDuration($filePath)
{
    exec('ffmpeg -i'." '$filePath' 2>&1 | grep Duration | awk '{print $2}' | tr -d ,",$O,$S);
    if(!empty($O[0]))
    {
        return $O[0];
    }else
    {
        return false;
    }
}

Step 3: To get dimensions of video

 public static function getAVWidthHeight( $filePath )
{
    exec("ffprobe -v error -show_entries stream=width,height -of default=noprint_wrappers=1 '$filePath'",$O,$S);
    if(!empty($O))
    {
        $list = [
                "width"=>explode("=",$O[0])[1],
                "height"=>explode("=",$O[1])[1],
        ];

        return $list;
    }else
    {
        return false;
    }
}
Red Bottle
  • 2,839
  • 4
  • 22
  • 59
-1

Yii2 implementation of getID3()

step1 : You need to download the class file from the below url

https://github.com/JamesHeinrich/getID3/archive/master.zip

step2 : extract it in to your vendor directory url look like this

vendor/getID3-master/getid3/getid3.php

step3: put below code to test the result

public function actionIndex() { //where you need this code

  $path = \Yii::getAlias("@vendor/getID3-master/getid3/getid3.php");
  require_once($path);
  $getID3 = new \getID3;
  $file = $getID3->analyze($path_of_the_video_file);

  echo("Duration: " . $file['playtime_string'] .
    " / Dimensions: " . $file['video']['resolution_x'] . " wide by " . $file['video']['resolution_y'] . " tall" .
    " / Filesize: " . $file['filesize'] . " bytes<br />"); 

 die;// for see instant result;

}
Dharmendra Singh
  • 1,186
  • 12
  • 22