How to get duration, size and input sizes of video files in Yii2?
I have a form that accepts a video file as 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 a video. There is no mention of video parsing in the yiiframework docs. Is there a way to do this?
Edit: As suggested, getID3 () exists to work with native php. Is there a way to do this in Yii2 without third party libraries? If not, how to integrate getID3 () in Yii2?
source to share
solvable.
Step 1: ( Install FFmpeg here )
Step 2: To get the duration of the 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 the dimensions of the 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;
}
}
source to share
Add "james-heinrich/getid3": "*"
to the require
section in the project-directory/composer.json
file, then in the project directory (if you installed composer well) run the command:
composer update
Then in your project use it:
$getID3 = new \getID3;
$file = $getID3->analyze($pathToFIle);
The complete library will be available in your project without unnecessary imports / requires.
source to share
Yii2 implementation of getID3 ()
step1: you need to download the class file from below url
step2: extract it to your url provider directory like below.
seller / getID3-master / getID3 / getid3.php
step3: put below code to check 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;
}
source to share