2

Hi we are using ffmpeg for compressing the video through php script, now what i need is i want to get the video size of compressed image, but am getting an video path so kindly guide me how i need to over come this?

Below is the code what i used for compressing the video

original path

$path = "sample.mp4";

Command to compress

exec("ffmpeg -i sample.mp4 -vcodec h264 -acodec aac -strict -2 compressed_video.mp4);

The command what i used for getting video size

$compressed_video_information = exec("ls -h1 compressed_video.mp4);
echo $compressed_video_information;

I get just file path instaed of getting video file size, so someone help me how to overcome this issue?

HMagdy
  • 3,029
  • 33
  • 54
Mani Kandan
  • 699
  • 1
  • 10
  • 30

2 Answers2

2

-h option (ex: ls -lh) displays size in human readable form(KB/MB/GB etc..)

exec("ls -lh compressed_video.mp4",$out);// pass file path here
$size=explode(' ',$out[0]);
print_r($size[4]);
Saty
  • 22,443
  • 7
  • 33
  • 51
0

Use the PHP function filesize:

<?php
$filesize_in_bytes = filesize('compressed_video.mp4');
Plenka
  • 1,099
  • 7
  • 17
  • this will take some more time for my execution. – Mani Kandan Jul 14 '16 at 08:50
  • It's also return 0 if file size is more then 2GB – Saty Jul 14 '16 at 09:48
  • @ManiKandan: if time is of importance then use `du`, as in `du compressed_video.mp4`. It's twice as fast as using `ls -lh`. However, I'm not sure how execution time of `filesize` can hinder you if the impact of it on the total execution time of your script will be of no significance; converting video will take a lot longer. It's also just as fast as using `ls`. @Saty: in the question no indication is made about the size of the resulting video files. A native PHP solution is also more robust, as the use of `ls` with exploding of the result depends on the configuration of the OS. – Plenka Jul 14 '16 at 13:16