By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,299 Members | 1,054 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,299 IT Pros & Developers. It's quick & easy.

Get video dimensions (width & height)

KeredDrahcir
100+
P: 426
Is there a simple way to get the dimensions of a video that's been uploaded. I've looked around and either find suggestions the use something else on the web that either doesn't work or doesn't exist.

I'm uploading a video and writing it to the server. I then want to find out its dimensions.

Something like this:
getsise($filename)

Can anyone help?
May 11 '10 #1

✓ answered by HaLo2FrEeEk

Ok, I didn't read the entire thread, but I read enough. The ffmpeg option will work, and you shouldn't even need ffmpeg-php, regular shell-based ffmpeg will work (that is, if you have a Linux server...not sure about Windows).

I recently needed to find the length of multiple video files (over 100) and I wasn't about to do it manually, so I found an alternative:

Expand|Select|Wrap|Line Numbers
  1. function getDuration($videofile) {
  2.   ob_start();
  3.   passthru("/usr/bin/ffmpeg -i \"". $videofile . "\" 2>&1");
  4.   $results = ob_get_contents();
  5.   ob_end_clean();
  6.  
  7.   preg_match("#Duration: ([\d]{2}):([\d]{2}):([\d]{2}).([\d]{1,2}), start#si", $results, $match);
  8.   $seconds = makeSeconds($match[1], $match[2], $match[3], $match[4]);
  9.   return $seconds;
  10.   }
Running the command (ffmpeg -i "videofile.mov" 2>&1) returns something like this:

Expand|Select|Wrap|Line Numbers
  1. FFmpeg version SVN-rUNKNOWN, Copyright (c) 2000-2007 Fabrice Bellard, et al.
  2.   configuration: --prefix=/usr --libdir=${prefix}/lib --shlibdir=${prefix}/lib --incdir=${prefix}/include/ffmpeg --enable-shared --enable-libmp3lame --enable-gpl --enable-libfaad --mandir=${prefix}/share/man --enable-libvorbis --enable-pthreads --enable-libfaac --enable-xvid --enable-libdts --enable-amr_nb --enable-amr_wb --enable-pp --enable-libogg --enable-libgsm --enable-x264 --enable-liba52 --enable-libtheora --extra-cflags=-Wall -g -fPIC -DPIC --cc=ccache cc --enable-swscaler
  3.   libavutil version: 49.4.0
  4.   libavcodec version: 51.40.2
  5.   libavformat version: 51.11.0
  6.   built on Feb  4 2008 14:49:27, gcc: 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)
  7. Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'videofile.mov':
  8.   Duration: 00:00:08.7, start: 0.000000, bitrate: 1483 kb/s
  9.   Stream #0.0(eng): Video: svq3, yuv420p, 640x360, 29.97 fps(r)
  10.   Stream #0.1(eng): Audio: mp3, 44100 Hz, stereo
  11. Must supply at least one output file
You can see in my function that I used regular expressions to find the duration, you can do this with the dimensions as well. I don't want to figure out the regular expression right now, but you'd want to go after the Video: text, since it only occurs once in the output, then just iterate through until you find the dimensions, which in this case are 640x360.

I hope this helps you out, it sure saved me a ton of time.

Share this Question
Share on Google+
28 Replies


dlite922
Expert 100+
P: 1,584
I don't think PHP can do this. Search for a program that does this, perhaps through a CLI. and just have PHP call it.

something like:

Expand|Select|Wrap|Line Numbers
  1.  
  2. exec("someprogram -f videofile.mpg",$output); 
  3.  
  4.  




Dan
May 11 '10 #2

KeredDrahcir
100+
P: 426
Can you suggest a program or how to do it through the command line? I have searched for other types of code that do it and couldn't find any which I why I came here.
May 12 '10 #3

Markus
Expert 5K+
P: 6,050
The ffmpeg-php extension may be of use to you. Are you able to add extensions to your server?
May 12 '10 #4

KeredDrahcir
100+
P: 426
I found a lot of references to ffmpeg-php. What is it? I couldn't seem to find out where to download it.
May 12 '10 #5

Markus
Expert 5K+
P: 6,050
Well, ffmpeg is a widely used video-editing library. Someone wrote the PHP extension for it (which just wraps the C library, and exposes it to PHP-land). However, the project was last updated 10 months ago. Not sure whether it's dead, or not.
May 12 '10 #6

KeredDrahcir
100+
P: 426
I seem to have downloaded a rar file. Do you know what I'm supposed to do with it?
May 13 '10 #7

Markus
Expert 5K+
P: 6,050
You're going to need to compile the extension. What OS are you on?
May 13 '10 #8

KeredDrahcir
100+
P: 426
I'm using Microsoft Windows XP Professional Version 2002.
May 13 '10 #9

Markus
Expert 5K+
P: 6,050
Do you have a C compiler at hand?
May 13 '10 #10

KeredDrahcir
100+
P: 426
Not on Windows but I have one on another OS available that uses Unix.
May 13 '10 #11

Markus
Expert 5K+
P: 6,050
Unfortunately it has to be compiled on the same OS it's going to be used, not sure if you could do cross-compilation from Unix to Windows. Visual Studio C++ Express is free.
May 13 '10 #12

KeredDrahcir
100+
P: 426
I wouldn't have thought the OS was an issue if I'm going to be using in a php application which has to be run on the web.
May 13 '10 #13

Markus
Expert 5K+
P: 6,050
However, the extension itself is written in C, and has the problem of being compiled on the OS it is intended to be used on.
May 13 '10 #14

KeredDrahcir
100+
P: 426
I guess I'll need to download Visual Studio C++ Express then or do I need to get it from somewhere?
May 13 '10 #15

KeredDrahcir
100+
P: 426
I've downloaded an installed a C complier for Windows. What do I need to do next?
May 14 '10 #16

Markus
Expert 5K+
P: 6,050
Which compiler do you have?
May 14 '10 #17

KeredDrahcir
100+
P: 426
Microsoft Visual C++ 2008 Express Edition. Thank you for all the time you're spending on this. I really do appreciate it.
May 14 '10 #18

Markus
Expert 5K+
P: 6,050
Anything for a fellow brit. ;)

Right. I think you may also need the re-distributable - don't worry, this one's a small download.

Now, to compile an extension, we need the PHP headers and the php5 library file. However, I'm assuming you downloaded the binary files from php.net, i.e., you didn't compile PHP yourself. If that is the case, we'll have to download the PHP source.

<break>

This can be quite hairy for a newcomer. You could, instead, find an ffmpeg binary that has already been compiled for windows, and then use one of the system functions in PHP to call the binary, as dlite922 suggested.

Now, we can either do the whole compile-from-source thing, or just use a pre-built binary. With the former, it's a tedious process and may also involve a PHP-upgrade or downgrade, depending on your current PHP version and what tags there are in the PHP SVN repository. Also, this would mean producing a new php.exe with either the php-ffmpeg extension statically compiled into the binary or compiled to a shared library (.dll). However, the benefits of this give you a well-defined PHP API to work with, as opposed to running the ffmpeg binary with command-line switches to get the desired output.

The second option is as simple as installing a compiled ffmpeg binary on your system, pointing your PATH environment variable to it, and then running system('ffmpeg -some -switches -etc); to get your desired functionality. Pros: simple to setup. Cons: no well-defined PHP API to use, involves using command line switches, etc.

Let me have a think of how this could be made easier.
May 14 '10 #19

KeredDrahcir
100+
P: 426
I've downloaded a Windows binary for ffmpeg to give my self a choice of what to do. The file extension is 7z. Does that sound right? I might have to use your second option but I'm a bit worried how it will affect the other websites on my server.
May 14 '10 #20

KeredDrahcir
100+
P: 426
I have had a think about this after being on holiday. I'm not too sure about ffmpef-php since the project is not up to date and there could be some problems with IMAP.

Would it help if you new my web server configuration details, or is there any other way to do this?
Is it possible to extract the funactionality of ffmpeg-php and put it into a php file?

Would the CLI method suggested by Dan work and if so would someone be able to help me through with that?
Jun 2 '10 #21

Dormilich
Expert Mod 5K+
P: 8,639
The file extension is 7z. Does that sound right?
that’s a zipped archive.

Is it possible to extract the funactionality of ffmpeg-php and put it into a php file?
it’s a shared extension, I guess there is a reason why …
Jun 2 '10 #22

KeredDrahcir
100+
P: 426
Is they a way to do this with JavaScript? I'm total willing to use JavaScript if that will work. I just think the ffmpeg-php is getting to complicated more me (and from what I can make out, far to risky).
Jun 2 '10 #23

HaLo2FrEeEk
100+
P: 404
Ok, I didn't read the entire thread, but I read enough. The ffmpeg option will work, and you shouldn't even need ffmpeg-php, regular shell-based ffmpeg will work (that is, if you have a Linux server...not sure about Windows).

I recently needed to find the length of multiple video files (over 100) and I wasn't about to do it manually, so I found an alternative:

Expand|Select|Wrap|Line Numbers
  1. function getDuration($videofile) {
  2.   ob_start();
  3.   passthru("/usr/bin/ffmpeg -i \"". $videofile . "\" 2>&1");
  4.   $results = ob_get_contents();
  5.   ob_end_clean();
  6.  
  7.   preg_match("#Duration: ([\d]{2}):([\d]{2}):([\d]{2}).([\d]{1,2}), start#si", $results, $match);
  8.   $seconds = makeSeconds($match[1], $match[2], $match[3], $match[4]);
  9.   return $seconds;
  10.   }
Running the command (ffmpeg -i "videofile.mov" 2>&1) returns something like this:

Expand|Select|Wrap|Line Numbers
  1. FFmpeg version SVN-rUNKNOWN, Copyright (c) 2000-2007 Fabrice Bellard, et al.
  2.   configuration: --prefix=/usr --libdir=${prefix}/lib --shlibdir=${prefix}/lib --incdir=${prefix}/include/ffmpeg --enable-shared --enable-libmp3lame --enable-gpl --enable-libfaad --mandir=${prefix}/share/man --enable-libvorbis --enable-pthreads --enable-libfaac --enable-xvid --enable-libdts --enable-amr_nb --enable-amr_wb --enable-pp --enable-libogg --enable-libgsm --enable-x264 --enable-liba52 --enable-libtheora --extra-cflags=-Wall -g -fPIC -DPIC --cc=ccache cc --enable-swscaler
  3.   libavutil version: 49.4.0
  4.   libavcodec version: 51.40.2
  5.   libavformat version: 51.11.0
  6.   built on Feb  4 2008 14:49:27, gcc: 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)
  7. Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'videofile.mov':
  8.   Duration: 00:00:08.7, start: 0.000000, bitrate: 1483 kb/s
  9.   Stream #0.0(eng): Video: svq3, yuv420p, 640x360, 29.97 fps(r)
  10.   Stream #0.1(eng): Audio: mp3, 44100 Hz, stereo
  11. Must supply at least one output file
You can see in my function that I used regular expressions to find the duration, you can do this with the dimensions as well. I don't want to figure out the regular expression right now, but you'd want to go after the Video: text, since it only occurs once in the output, then just iterate through until you find the dimensions, which in this case are 640x360.

I hope this helps you out, it sure saved me a ton of time.
Jun 4 '10 #24

KeredDrahcir
100+
P: 426
This looks like it could be the answer I'll have a play on Monday (after a weekend's rest) and let you know how I get on.
Many thanks.
Jun 4 '10 #25

KeredDrahcir
100+
P: 426
Thanks for your help HaLo2FrEeEk. Is there anyone who knows if there is a shell based ffmpeg for Windows or how I can run one from Windows or am I going back to sqaure 1?
Jun 4 '10 #26

HaLo2FrEeEk
100+
P: 404
I did a quick search and found this page:

http://ffmpeg.arrozcru.org/autobuilds/

You'll want to download the most recent build of ffmpeg. Is your Windows server your own computer, or are you at a hosting company? Are you on dedicated hosting or shared? You'll need to know these things, but ultimately you should be able to run the binary.

Basically, download the executable to a root folder, outside of the scope of the actual website, then use command prompt to use it. The code in PHP should be the same, exec().

I'm not absolutely 100% sure on this because I'm on a Linux server which has ffmpeg installed natively.
Jun 5 '10 #27

KeredDrahcir
100+
P: 426
Thanks for your help. I'll give this a go and see what happens.
Jun 7 '10 #28

KeredDrahcir
100+
P: 426
I think I've found another solutuion.
I found this code
Expand|Select|Wrap|Line Numbers
  1. for filename in $(ls *mpg *avi)
  2.     do mplayer -nosound -vo null -ss 03:00:00 -really-quiet -identify $filename
  3.     echo -e "\n**************\n"
  4.     done >>info.txt
I don't know if this will get the information I want. Will this help me and if so, how do I get the Internet to talk to the command promt?
Jul 5 '10 #29

Post your reply

Sign in to post your reply or Sign up for a free account.