Request audio / video file for information

I need a PHP function that gets the file path and returns an array of information about it. The PHP file can call FFmpeg.

The returned data should be something like

Array( [mime] => video/ogg [container] => Ogg [video] => Theora [audio] => Vorbis [duration] => 20.3 // in seconds ) Array( [mime] => audio/ogg [container] => Ogg [video] => [audio] => FLAC [duration] => 3 ) Array( [mime] => image/gif [container] => GIF [video] => Animated GIF [audio] => [duration] => 2 ) Array( [mime] => video/webm [container] => WebM [video] => VP8 [audio] => Vorbis [duration] => 900.7 ) false // not a media file 

I have never worked with FFmpeg or the PHP function shell_exec() , but it seems that FFmpeg will provide information about the video (or audio files) in a rather hard format. I suppose something like this is possible.

+4
source share
4 answers

FFmpeg can do almost anything you want, but you are right that it is almost impossible to parse the format. This is faster if you use a UNIX-based system to use the operating system and FFmpeg functions for parsing, and not for PHP to try to understand the result. A few months ago, I developed the following shell command for a similar purpose.

 fulltime=`ffmpeg -i MOVIE.AVI 2>&1 | grep 'Duration' | cut -d ' ' -f 4 | sed s/,//`;hour=`echo $fulltime | cut -d ':' -f 1`;minute=`echo $fulltime | cut -d ':' -f 2`;second=`echo $fulltime | cut -d ':' -f 3 | cut -d '.' -f 1`;echo `expr 3600 \* $hour + 60 \* $minute + $second` 

The pipe ( | ) tells UNIX to pass the output of the previous command as input to the next command. A semi-colon ( ; ) tells UNIX to start a new command. The UNIX shell also allows you to create variables on the fly. To create a variable, just say var=value . To call this variable, you must use a dollar sign, for example $var .

My team takes the output ffmpeg -i MOVIE.AVI 2>&1 | grep 'Duration' | cut -d ' ' -f 4 | sed s/,// ffmpeg -i MOVIE.AVI 2>&1 | grep 'Duration' | cut -d ' ' -f 4 | sed s/,// ffmpeg -i MOVIE.AVI 2>&1 | grep 'Duration' | cut -d ' ' -f 4 | sed s/,// and save it as fulltime . This command is further broken down:

ffmpeg -i MOVIE.AVI 2>&1 means displaying information ( -i ) for MOVIE.AVI. 2>&1 I believe, redirected the output to the channel, not to the screen (did it forever, so I do not understand)

grep 'Duration' will accept the input (which was the result of ffmpeg -i MOVIE.AVI 2>&1 ), find the word "Duration" and display only the line containing this word.

cut -d ' ' -f 4 will accept the input (output of the previous statement), split it in each space ( ' ' ) and output only the fourth part. This is because the output of FFmpeg looks something like this:

 MOVIE.AVI video encoder, yada yada audio encoder, yada yada bitrates and stuff Duration: hh:mm:ss fulltime: 00:30:00.1 
In other words, the fourth β€œsection” of the line containing β€œDuration” is the actual duration ... In hours, minutes and seconds (with 1 decimal point).

sed s/,// means replace "," with nothing. There was probably a good reason for this.

After that I created three variables hour , minute and second :

 hour=`echo $fulltime | cut -d ':' -f 1`; minute=`echo $fulltime | cut -d ':' -f 2`; second=`echo $fulltime | cut -d ':' -f 3 | cut -d '.' -f 1`; 

Note that I set the hour to output echo $fulltime | cut -d ':' -f 1 echo $fulltime | cut -d ':' -f 1 . Capturing the first part, if I have to divide the time in each colon ( :

I do similar minutes, then I do the same with seconds, only I chop off any decimal digit at the end. This decimal value should be removed if you are working in a shell, since the expr command (which I used to calculate seconds from hours, minutes, and seconds) does only integer math. If you want to store the decimal value for the duration, you must return the hours, minutes and seconds to PHP at this point and parse. This would be easiest to do by replacing the above:

 echo $fulltime 

This will return a string, such as "00: 30: 00.1", in PHP after calling shell_exec() , which you can easily explode(':',$string) with explode(':',$string) .

To continue receiving seconds directly from the shell:

After receiving hours, minutes and seconds, a magic line appeared:

 echo `expr 3600 \* $hour + 60 \* $minute + $second` 

The expr command says it is performing a mathematical expression. Unfortunately, this is pretty stupid. First, special characters must be escaped ( * is a wildcard on UNIX, so you must use \* to indicate multiplication). Then he does just a whole math. It was quite easy, as you can see, to multiply the hours by 3600, 60 minutes and add them all together.

The result of this massive team will be just one number. The number of seconds the video lasts. It should work for all video formats.

Remember, this is just a duration. As for the video encoder and audio encoder, you would like to use similar methods, minus the math at the end. A general solution would be as follows:

 ffmpeg -i MOVIE.AVI 2>&1 | grep 'Something unique to the line you want' | cut -d ' ' -f <the "section" you want> 

The result of this can either continue in the parsing in the shell (as I did above with time), or it can be passed to PHP.

+9
source

I would look at Mediainfo . If you go into --Output = XML, it will give you something like this:

 <?xml version="1.0" encoding="UTF-8"?> <Mediainfo> <File> <track type="General"> <Complete_name>Movie.webm</Complete_name> <Format>Matroska</Format> <File_size>215 MiB</File_size> <Duration>28mn 39s</Duration> <Overall_bit_rate>1 049 Kbps</Overall_bit_rate> <Movie_name>Safari</Movie_name> <Writing_application>Lavf52.78.0</Writing_application> <Writing_library>Lavf52.78.0</Writing_library> </track> <track type="Video"> <ID>1</ID> <Format>V_VP8</Format> <Codec_ID>V_VP8</Codec_ID> <Duration>28mn 39s</Duration> <Bit_rate>529 Kbps</Bit_rate> <Width>768 pixels</Width> <Height>432 pixels</Height> <Display_aspect_ratio>16:9</Display_aspect_ratio> <Frame_rate>25.000 fps</Frame_rate> <Bits__Pixel_Frame_>0.064</Bits__Pixel_Frame_> <Stream_size>108 MiB (50%)</Stream_size> <Language>Norwegian</Language> </track> <track type="Audio"> <ID>2</ID> <Format>Vorbis</Format> <Format_settings__Floor>1</Format_settings__Floor> <Codec_ID>A_VORBIS</Codec_ID> <Duration>28mn 39s</Duration> <Bit_rate_mode>Constant</Bit_rate_mode> <Bit_rate>500 Kbps</Bit_rate> <Channel_s_>2 channels</Channel_s_> <Sampling_rate>48.0 KHz</Sampling_rate> <Resolution>16 bits</Resolution> <Stream_size>102 MiB (48%)</Stream_size> <Writing_library>libVorbis 20090709 (UTC 2009-07-09)</Writing_library> <Language>Norwegian</Language> </track> </File> </Mediainfo> 
+2
source

You can try this guy. It is always good when you can use the library: http://www.phpclasses.org/browse/file/1582.html

+1
source

Found http://getid3.sourceforge.net/ when searching for php audio parser

Hope this helps,

Chairs

0
source

All Articles