sanjayacloud's avatar

How to get video duration while upload video file

Hi Guys,

I want to know, How I can get the video duration while uploading the video in laravel? Anyone can help me on this?

0 likes
1 reply
Tray2's avatar

While you upload is a bit tricky since it requires certain software installed on the users computer.

However you can use ffmpeg to get loads of information after the upload is done.

https://github.com/PHP-FFMpeg/PHP-FFMpeg

You can of course do it from the command line as well and not use the library.

Here is a little script that I made a while back that takes all the media files in a directory and recodes them at a lower resolution.

<?php
  $dir = $argv[1];
  $list = $argv[2];
function getDirContents($dir, &$results = array()){
    $files = scandir($dir);

    foreach($files as $key => $value){
        $path = realpath($dir.DIRECTORY_SEPARATOR.$value);
        if(!is_dir($path)) {
            $results[] = $path;
        } else if($value != "." && $value != "..") {
            getDirContents($path, $results);
            $results[] = $path;
        }
    }

    return $results;
}

function getMediaFiles($contentList)
{
	$allowedTypes = ['mpg', 'mp4', 'mkv', 'flv', 'mov', 'mpeg', 'avi', 'wmv'];
	$mediaFiles = [];

	foreach($contentList as $row) {
		if(is_file($row)) {
			$extension = pathinfo($row, PATHINFO_EXTENSION);
			if(in_array($extension, $allowedTypes)) {
				$mediaFiles[] = $row;
			}
		}
	}

	return $mediaFiles;
}

function getMediaFilesToResize($mediaList)
{
	$mediaToResize = [];
	foreach($mediaList as $row) {
		$command = 'ffmpeg -i ' . str_replace(' ', '\ ', $row) . ' 2>&1 | grep Video: | grep -Po \'\d{3,5}x\d{3,5}\' | cut -d\'x\' -f2';
		$height = (int)exec($command);
        if ($height > 720) {
            $mediaToResize[] = $row;
        }
	}
	return $mediaToResize;
}

function startResize($mediaToResize)
{
	$num = 1;
	$total = count($mediaToResize);
	$originalSize = 0;
	$newSize = 0;
	
	foreach($mediaToResize as $row) {
		$extension = pathinfo($row, PATHINFO_EXTENSION);
		$filename = trim(str_replace(' ', '_', basename($row, $extension)), '.');
		echo "Processing file $num of $total\n";
	    echo "Starting conversion of $filename\n";
		$originalSize += filesize($row);
		$command = 'ffmpeg -loglevel quiet -stats -i ' . str_replace(' ', '\ ', $row) . ' -vf scale=1280:720 ' . str_replace(' ', '\ ',pathinfo($row, PATHINFO_DIRNAME)) .'/' . $filename . '-720p.' . $extension;
		exec($command);
		unlink($row);
		$newSize += filesize(pathinfo($row, PATHINFO_DIRNAME).'/' . $filename . '-720p.' . $extension);
		$num++;
		echo "Done processing $row!\n";
	}

	return ['old_size' => $originalSize, 'new_size' => $newSize];
}

function getEarnedSpace($result)
{
	return round(($result['old_size'] - $result['new_size']) / 1024 / 1024 / 1024, 3);
}

function main($dir, $list = null)
{
	$files = getDirContents($dir);
	$mediaFiles = getMediaFiles($files);
	$mediaToResize = getMediaFilesToResize($mediaFiles);
    if ($list == null) {
        $result = startResize($mediaToResize);
        $freedSpace = getEarnedSpace($result);
        echo "Conversion Finshed\n";
        echo "You have freed up $freedSpace Gb of space on your harddrive\n";
    } else {
		print_r($mediaToResize);
	}
}

main($dir, $list);

Please or to participate in this conversation.