472,353 Members | 1,818 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

Get video dimensions (width & height)

KeredDrahcir
426 256MB
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.

28 16961
dlite922
1,584 Expert 1GB
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
426 256MB
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
6,050 Expert 4TB
The ffmpeg-php extension may be of use to you. Are you able to add extensions to your server?
May 12 '10 #4
KeredDrahcir
426 256MB
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
6,050 Expert 4TB
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
426 256MB
I seem to have downloaded a rar file. Do you know what I'm supposed to do with it?
May 13 '10 #7
Markus
6,050 Expert 4TB
You're going to need to compile the extension. What OS are you on?
May 13 '10 #8
KeredDrahcir
426 256MB
I'm using Microsoft Windows XP Professional Version 2002.
May 13 '10 #9
Markus
6,050 Expert 4TB
Do you have a C compiler at hand?
May 13 '10 #10
KeredDrahcir
426 256MB
Not on Windows but I have one on another OS available that uses Unix.
May 13 '10 #11
Markus
6,050 Expert 4TB
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
426 256MB
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
6,050 Expert 4TB
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
426 256MB
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
426 256MB
I've downloaded an installed a C complier for Windows. What do I need to do next?
May 14 '10 #16
Markus
6,050 Expert 4TB
Which compiler do you have?
May 14 '10 #17
KeredDrahcir
426 256MB
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
6,050 Expert 4TB
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
426 256MB
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
426 256MB
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
8,658 Expert Mod 8TB
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
426 256MB
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
404 256MB
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
426 256MB
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
426 256MB
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
404 256MB
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
426 256MB
Thanks for your help. I'll give this a go and see what happens.
Jun 7 '10 #28
KeredDrahcir
426 256MB
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

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

Similar topics

5
by: S | last post by:
Whew! Thanks for your help on that last post. I actually understand what you wrote. I'm trying not to use these groups as a way to get my code...
2
by: Dave | last post by:
Dear Sirs, Is there a way to get the width and height of an image without downloading the image, or with just downloading a minimal portion of...
1
by: Dundappa | last post by:
hi all, Can any one help me in setting the page width & height in asp.net (language used is vb) Thanks in anticipation.
2
by: ayoubahmed | last post by:
Hi,, Im trying to print invoices in VB.net, and the invoice is 8.5 x 8.5 inches. Now i have to specify the new height, but when i do specify it...
2
by: Farce Milverk | last post by:
Hi, I'm looking for an algorithm to resize an image of arbitrary size to a "fixed" / required width and height. For example, my application...
2
by: emma.sax | last post by:
My script is as follows: function setImageSizes() { var staticHeight = 70; if(!document.getElementsByTagName || !document.images) return...
2
by: cooldudevamsee | last post by:
HI fellows, How can I get visible viewport dimensions of any browser ?
1
by: gopim | last post by:
i uploaded image into database.but i want to restrict if images having more size(width & height) before uploading into database. could any please...
4
chelvan
by: chelvan | last post by:
hi all i want to know is it possible to define a browsers width & height through php? if so how is it? thanks chel-1
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.