Ask your WordPress questions! Pay money and get answers fast! (more info)

a single php function to get a video dimension WordPress

  • SOLVED

Hi there,
I am looking for someone can help me with writing a single php function to get a video dimension in my wordpress theme. Input (parameter) will be url of the video, and return value can be something like array[width,height]. I intend to use this function in my shortcode which inserts a video in posts. I want this shortcode to automatically read video dimension and generate the proper code with dimension for that purpose.

Answers (5)

2013-06-17

Daniel Yoen answers:

hello,

you can try this :

function video_code($atts, $content=null)
{
extract(
shortcode_atts(array(
'url' => '',
'width' => '400',
'height' => '200'
), $atts)
);
return '<iframe width="' . $width . '" height="' . $height . '" src="' . $url . '" class="videos"></iframe>';
}
add_shortcode('videos','video_code');


usage :

[videos url="http://example.com/video.avi" width="400" height="200"]

hope this help :-)


tsynsth comments:

Oh this looks promising for me, let me try it to see if that works...


tsynsth comments:

hum, now i have make sure that i am NOT asking a question how to make a shortcode to insert a video on posts, but tyring to get to know how to get a dimension of video by its file url.
sorry this answer seems to me something different from waht i expected.


tsynsth comments:

thanks for your quick answer though.


Daniel Yoen comments:

Sorry, I think you need : http://ffmpeg-php.sourceforge.net/


$movie = new ffmpeg_movie("filepath");

$width = $movie->getFrameWidth();
$height = $movie->getFrameHeight();


Hope this help :-)


tsynsth comments:

Sure thanks again for your quick reply.

i had found that resource http://ffmpeg-php.sourceforge.net/ before i started this thread and i agree that will help me to get some meta values from video with using its class, is that correct?
However, the site looks too intricate to me and i am not sure how i actually implement this in my wordpress theme.

may i ask you, if i insert the 3-lines code you have posted above, will it work right away? or do i need to do something extra? like "include" some files?
thanks in advance,


2013-06-17

Giri answers:

Are you talking about image dimension?


tsynsth comments:

i am talking about a VIDEO dimension (resolution). thank you.


Giri comments:

I still don't understand your question...

<blockquote>Input (parameter) will be url of the image</blockquote>

What image?

Why do you want to get height and width of the video? I think you still didn't explain your question clearly.

I think you should try responsive video embeds plugin.

http://wordpress.org/plugins/responsive-video-embeds/

It will adjust the height and width automatically when you resize the window or view from tablet and mobile devices..

You have to use shortcode like this..

[embed]http://www.youtube.com/watch?v=sSx1dYJlJh4[/embed]


Giri comments:

Also note [embed] is wordpress built-in shortcode. You can use following sites

<blockquote>blip.tv
DailyMotion
Flickr (both videos and images)
FunnyOrDie.com (WordPress 3.0+)
Hulu
Instagram (WordPress 3.5+)
Qik
Photobucket
PollDaddy
Revision3
Scribd
SlideShare (WordPress 3.5+)
SoundCloud (WordPress 3.5+)
SmugMug (WordPress 3.0+)
Twitter (WordPress 3.4+)
Viddler
Vimeo (note older versions of WP have issues with https embeds, just remove the s from the https to fix)
YouTube (only public and "unlisted" videos and playlists - "private" videos will not embed)
WordPress.tv (only VideoPress-type videos for the time being)</blockquote>

For more details..

http://codex.wordpress.org/Embeds


Giri comments:

Try this code

[[LINK href="http://danismad.tumblr.com/post/74414011/get-size-of-the-video-with-php-and-ffmpeg-and-disabled"]]http://danismad.tumblr.com/post/74414011/get-size-of-the-video-with-php-and-ffmpeg-and-disabled[[/LINK]]


tsynsth comments:

Regarding "Input (parameter) will be url of the image", sorry that was my mistake.

I intend to use this function in my shortcode which inserts a video in posts. I want this shortcode to automatically read video dimension and generate the proper code with dimension for the purpose.

I will check that plugin you have suggested but i still would like to figure out how to get the dimension of a video as a simple process like getting the dimension of an image by the use of getimagesize($filename);


Giri comments:

Check my previous answer


tsynsth comments:

sure, that looks interesting. i will return after checking the code. thank you.


tsynsth comments:

Hi Giri,

After reviewing the site you provided me with above, I have tried putting the function in function.php and a line of code on one of page template, trying to display the return values of the function, width and height of a video, on "test-output' page.
However, I couldnt have a good result with that.
You can see it from here.

http://tuningsynesthesia.com/en/test-output/

Would you mind if i ask you to look at those codes i have put there and tell me where you think i wrongly implement the function to my theme? there is a line "$ffmpeg_path = 'ffmpeg';" and "global $ffmpeg_path;". do i have to include files so that this function works properly?

Thank you.

Here is the code I put in function.php;


//max width of the video.
//If video width more than this value return
//new size of the video with original proportion
define('MAX_VIDEO_WIDTH', 200);
$ffmpeg_path = 'ffmpeg';
/**
* method to get size of the video
* @param $videofile path of the video
* @returns Array(width, height)
*/
function get_video_size($videofile){
global $ffmpeg_path;
$duration = array();
$bitrate = '';
$video_bitrate = '';
$vwidth = 320;
$vheight = 240;
$owidth = '';
$oheight = '';
$sfrequency = '';
$audio_bitrate = '';

$ffmpeg_output = array();
$ffmpeg_cmd = $ffmpeg_path . " -i '" . $videofile . '\' 2>&1 | cat | egrep -e \'(Duration|Stream)\'';
@exec($ffmpeg_cmd, $ffmpeg_output);

// if file not found just return null
if(sizeof($ffmpeg_output) == '')return null;

foreach($ffmpeg_output as $line){
$ma = array();
// get duration and video bitrate
if(strpos($line, 'Duration:') !== false){
preg_match('/(?<hours>\d+):(?<minutes>\d+):(?<seconds>\d+)\.(?<fractions>\d+)/', $line, $ma);
$duration = array(
'raw' => $ma['hours'] . ':' . $ma['minutes'] . ':' . $ma['seconds'] . '.' . $ma['fractions'],
'hours' => intval($ma['hours']),
'minutes' => intval($ma['minutes']),
'seconds' => intval($ma['seconds']),
'fractions' => intval($ma['fractions']),
'rawSeconds' => intval($ma['hours']) * 60 * 60 +
intval($ma['minutes']) * 60 + intval($ma['seconds']) +
(intval($ma['fractions']) != 0 ? 1 : 0)
);

preg_match('/bitrate:\s(?<bitrate>\d+)\skb\/s/', $line, $ma);
$bitrate = $ma['bitrate'];
}

// get video size
if(strpos($line, 'Video:') !== false){
preg_match('/\s(?<width>\d+)[x](?<height>\d+)\s\[/', $line, $ma);
$owidth = $ma['width'];
$oheight = $ma['height'];
$vwidth = $owidth;
$vheight = $oheight;
}

// get audio quality
if(strpos($line, 'Audio:') !== false){
preg_match('/,\s(?<sfrequency>\d+)\sHz,/', $line, $ma);
$sfrequency = $ma['sfrequency'];

preg_match('/,\s(?<bitrate>\d+)\skb\/s/', $line, $ma);
$audio_bitrate = $ma['bitrate'];
}
}

// calculate new size of the video
if($vwidth > MAX_VIDEO_WIDTH){
$coef = $vheight / $vwidth;
$vwidth = MAX_VIDEO_WIDTH;
$vheight = round($vwidth * $coef);
}

// frame size must be a multiple of 2
$vwidth = $vwidth % 2 != 0 ? $vwidth - 1 : $vwidth;
$vheight = $vheight % 2 != 0 ? $vheight - 1 : $vheight;

// end of image size detection
// return all information about video and
// data about new size with originally proportion
return array(
'width' => $vwidth,
'height' => $vheight,
'srcWidth' => $owidth,
'srcHeight' => $oheight,
'duration' => $duration,
'bitrate' => $bitrate,
'audioBitrate' => $audio_bitrate,
'audioSampleFrequency' => $sfrequency
);
} //



Here is the code I put in page-test.php;


<?php
$size = get_video_size('http://tuningsynesthesia.com/en/wp-content/uploads/sites/2/post/2008_lapratt-digital-inoutput-ver-1-button-and-led/ASM03_lapratt-digital-inoutput-ver-1.mp4');
echo "<h3>width:" . $size['width'] . "</h3><h3>height:" . $size['height'] . "</h3>";
?>




Giri comments:

You should define your path there.

It should be something like

$ffmpeg_path = '/usr/bin/ffmpeg/ffmpeg';


Giri comments:

Also make sure you have ffmpeg.

Just use this test..

function ffmpegtest() {
$ffmpeg = trim(shell_exec('type -P ffmpeg'));
if (empty($ffmpeg))
{
die('ffmpeg not available');
}
}


Call that function anywhere in your template file.

If ffmpeg not enabled ask your hosting support to enable it..


Giri comments:

Much better version

function ffmpegtest() {
$ffmpeg = trim(shell_exec('type -P ffmpeg'));
if (empty($ffmpeg)) {
die('ffmpeg not available');
} else {
die('ffmpeg installed');
}
}


tsynsth comments:

Sure, thank you. i am still trying it now.


tsynsth comments:

I tried all giri's suggestions above however i had no luck with getting it work. i trust his solution but i contacted host server and they answered that ffmpeg isn't available on the account i am currently on. so i am assuming that this solution may not work unless i upgrade my server to dedicated server, which i cannot afford for the moment.

is there anything i am missing for making it work? or maybe may i start asking another solution to make it work? comparing to getting the dimension of an image, i feel getting the dimension of a video is much more complicated and don't get it why...

Any suggestions would be greatly appreciated.
Thank you


Giri comments:

Hello there, By default php has built-in functions for image filetypes. But videos are not possible without codec (coder/decoder) ffmpeg is a codec. So to get dimensions of your videos you must have ffmpeg installed. If its absolutely necessary then get a vps. Its cheaper than dedicated server. You will get good vps for $40/month. You can install any extensions in your vps


tsynsth comments:

Hi giri, thank you so much for your suggestion and explanation about ffmpeg. I think at this time i may have to give up making this work. but i would like to thank you for all the effort teaching me a new thing.


Giri comments:

You are welcome mate. If you are satisfied with my answer don't forget to vote. :)

2013-06-17

Hariprasad Vijayan answers:

Hello,

Can you please show the url.


tsynsth comments:

http://tuningsynesthesia.com/en/

The site is still under construction. I turned off "coming soon" mode for now.
I intend to use this single function in my shortcode.

2013-06-17

idt answers:

Hello,

Please check this out: http://stackoverflow.com/questions/4847752/how-to-get-video-duration-dimension-and-size-in-php

idt

2013-06-17

Arnav Joy answers:

do you have any fix source of video like youtube or vimeo etc?


tsynsth comments:

I am not sure what you mean by "any fix source of video", however, I upload a video from the button "add media" on post editing page and not using any sources from youtube or vimeo etc. thank you.