Go
Not registered? Sign up!

External PHP with FFmpeg using readfile()

Google Translate
22 posts | return to the Modules forum | get the rss feed for this thread

Aug. 28, 2008Mike

Hi all!!

I have an external thumbnail.php class which successfully grabs a screenshot of an FLV using FFmpeg. It's a long winded process, and i'm not really allowed to show all my code as it's part of our content management system.

Basically I have a flashvar for the image, which looks like this

&image=thumbnail.php?&filename=video.flv

The thumbnail.php returns an image using:

readfile($this->FileName);

Unfortunately, the FLV player doesn't seem to pick the resulting image up and display it. If I set an img tag calling thumbnail.php as the src like below, it displays the thumbnail just fine. Is it IS actually working and pulling the thumbnail, but the FLV player doesnt seems like it is recognizing it

<img src="thumbnail.php?&filename=video.flv" />

Any help would be appreciated.

Sep. 01, 2008Lakio

try <img src="thumbnail.php?&filename=video.flv&x=x.jpg" />

Sep. 01, 2008kLink


image=thumbnail.php%3Ffilename%3Dvideo.flv%26name%3Dimage.jpg&...the rest of your flashvars


The %26name%3Dimage.jpg is just to fool the player into thinking that it is a jpg image (which it really is). You could probably end the image flashvar with just %26jpg which, unencoded is: image=thumbnail.php?filename=video.flv&jpg

Sep. 04, 2008Mike Burns

thanks for the suggestions, however, it is still not working, not for the image nor for the actual file. I'm using SWFObject, so the exact code is thus:


<div id="container2">

<script language="javascript" type="text/javascript">
var s1 = new SWFObject("mediaplayer.swf?"+new Date().getTime(),"mediaplayer","110","82","8");
s1.addParam("allowfullscreen","true");
s1.addVariable("width","110");
s1.addVariable("height","82");
s1.addVariable("shownavigation ","false");
s1.addVariable("image","get_thumb.php?objectid=411&imgonly&big&file=file.jpg");
s1.addVariable("file","get_thumb.php?objectid=411&file=file.mp3");
s1.write("container2");
</script>


and as for get_thumb.php, it basically takes in the objectid and spits out a "thumbnail" version. In the first case, the imgonly flag tells the script simply to get a image, while the second one will output an mp3 file that DOES EXIST (I've checked many times)

the file outputs only one HTTP header, the content-type. Should I use other headers, or no headers, or should I output the file in certain sized blocks or something? I've been trying for a while and always come back to this fundamental problem. And I'm trying to release the site real soon as well.

Thanks for reading.

Sep. 05, 2008kLink


<script language="javascript" type="text/javascript">
var s1 = new SWFObject("mediaplayer.swf?"+new Date().getTime(), "mediaplayer", "110", "82", "8");
    s1.addParam("allowfullscreen",     "true");
    s1.addVariable("width",            "110");
    s1.addVariable("height",           "82");
    s1.addVariable("shownavigation",   "false");
    s1.addVariable("image",             encodeURIComponent("get_thumb.php?objectid=411&imgonly&big&file=file.jpg"));
    s1.addVariable("file",              encodeURIComponent("get_thumb.php?objectid=411&file=file.mp3"));
    s1.write("container2");
</script>

Sep. 05, 2008Mike Burns

Hey, thanks a lot, kLink. I just assumed that SWFObject would encode the strings for me. Guess not. I hope others can learn from my mistake.

Take it easy.

Sep. 05, 2008kLink

@Mike,

You're welcome. Good Luck! wink

Oct. 11, 2008Ashley

I don't suppose you would be willing to share your script would you?

Oct. 11, 2008JohnK

@Ashley,

What Script are you looking for? We share all of them.

Oct. 21, 2008aguante

I need the script to generate thumbnails from videos, will you upload it?

Oct. 21, 2008kLink

Windows:
<?php

// generate a preview image from an FLV file on-the-fly, or to save
// call with: ffmpeg_image.php?file=video.flv&time=00:00:05&browser=true
// call with: ffmpeg_image.php?file=video.flv&percent=75.3&browser=true
// no time defaults to "00:00:01" (one second), no browser defaults to "true"

$videofile = (isset($_GET['file'])) ? strval($_GET['file']) : 'video.flv';
$image     = substr($videofile, 0, strlen($videofile) - 4);
$time      = (isset($_GET['time'])) ? strval($_GET['time']) : '00:00:01';

// debug ("    File: ", $videofile);
// debug ("   Image: ", $image);
// debug ("    Time: ", $time);
// print "ffmpeg -vcodec png -i \"" . $videofile . "\" -ss " . $time . " -vframes 1 -f image2 -";
// exit;
// debug


// check time format
if (!preg_match('/\d\d:\d\d:\d\d/', $time))
{
  $time = "00:00:00";
}

// debug ("    Time: ", $time);

if (isset($_GET['percent']))
{
  $percent = $_GET['percent'];

  // debug (" Percent: ", $percent);

  ob_start();
  passthru("ffmpeg-10141.exe -i \"". $videofile . "\" 2>&1");
  $duration = ob_get_contents();
  ob_end_clean();

  // debug ("Duration: ", $duration);

  preg_match('/Duration: (.*?),/', $duration, $matches);
  $duration = $matches[1];

  // debug ("Duration: ", $duration);

  $duration_array = split(':', $duration);
  $duration = $duration_array[0] * 3600 + $duration_array[1] * 60 + $duration_array[2];
  $time = $duration * $percent / 100;

  // debug ("    Time: ", $time);

  $time = intval($time/3600) . ":" . intval(($time-(intval($time/3600)*3600))/60) . ":" . sprintf("%01.3f", ($time-(intval($time/60)*60)));

  // debug ("    Time: ", $time);

}

$browser = (isset($_GET['browser'])) ? strval($_GET['browser']) : 'true';

// debug (" Browser: ", $browser);

if ($browser == 'true')
{
  header('Content-Type: image/png');
  passthru("ffmpeg-10141.exe -vcodec png   -i \"" . $videofile . "\" -ss " . $time . " -vframes 1 -f image2 -");
  // header('Content-Type: image/jpeg');
  // passthru("ffmpeg-10141.exe -vcodec mjpeg -i \"" . $videofile . "\" -ss " . $time . " -vframes 1 -f image2 -");
}
else
{
  passthru("ffmpeg-10141.exe -vcodec png   -i \"" . $videofile . "\" -ss " . $time . " -vframes 1 -f image2 \"" . $image . "\"%d.png");
  // passthru("ffmpeg-10141.exe -vcodec mjpeg -i \"" . $videofile . "\" -ss " . $time . " -vframes 1 -f image2 \"" . $image . "\"%d.jpg");
}


function debug($text1, $text2)
{
  print "<pre>\n";
  print $text1 . $text2 . "\n";
  print "</pre>\n";
}

?>


*nix:
<?php

// generate a preview image from an FLV file on-the-fly, or to save
// call with: ffmpeg_image.php?file=video.flv&time=00:00:05&browser=true
// call with: ffmpeg_image.php?file=video.flv&percent=75.3&browser=true
// no time defaults to "00:00:01" (one second), no browser defaults to "true"

$videofile = (isset($_GET['file'])) ? strval($_GET['file']) : 'video.flv';
$image = substr($videofile, 0, strlen($videofile) - 4);
$time = (isset($_GET['time'])) ? strval($_GET['time']) : '00:00:01';

// debug ("  File: ", $videofile);
// debug (" Image: ", $image);
// debug ("  Time: ", $time);

// check time format
if (!preg_match('/\d\d:\d\d:\d\d/', $time))
{
  $time = "00:00:00";
}

if (isset($_GET['percent']))
{
  $percent = $_GET['percent'];

// debug (" Percent: ", $percent);

  ob_start();
  exec("/usr/bin/ffmpeg -i \"". $videofile . "\" 2>&1");
  $duration = ob_get_contents();
  ob_end_clean();

  // debug ("Duration: ", $duration);

  preg_match('/Duration: (.*?),/', $duration, $matches);
  $duration = $matches[1];

// debug ("Duration: ", $duration);

  $duration_array = split(':', $duration);
  $duration = $duration_array[0] * 3600 + $duration_array[1] * 60 + $duration_array[2];
  $time = $duration * $percent / 100;

// debug (" Time: ", $time);

  $time = intval($time/3600) . ":" . intval(($time-(intval($time/3600)*3600))/60) . ":" . sprintf("%01.3f", ($time-(intval($time/60)*60)));

// debug (" Time: ", $time);

}

$browser = (isset($_GET['browser'])) ? strval($_GET['browser']) : 'true';

// debug (" Browser: ", $browser);

if ($browser == "true")
{
  header('Content-Type: image/png');
  exec("/usr/bin/ffmpeg -vcodec png -i \"" . $videofile . "\" -ss " . $time . " -vframes 1 -f image2 -");
//header('Content-Type: image/jpeg');
//exec("/usr/bin/ffmpeg -vcodec mjpeg -i \"" . $videofile . "\" -ss " . $time . " -vframes 1 -f image2 -");
}
else
{
  exec("/usr/bin/ffmpeg -vcodec png -i \"" . $videofile . "\" -ss " . $time . " -vframes 1 -f image2 \"" . $image . "\"%d.png");
//exec("/usr/bin/ffmpeg -vcodec mjpeg -i \"" . $videofile . "\" -ss " . $time . " -vframes 1 -f image2 \"" . $image . "\"%d.jpg");
}

?>


There a few lines of code (3) near the end of the script that output PNGs. If you want JPGs, comment out the PNG code, and uncomment the corresponding JPG code.

Dec. 10, 2008rachel

rachel is looking for the embedded flv in

'flashVars="serverName=video2.channelnewsasia.com&folderName=chivideos&fileName=xin_032607_analysiswh.fl/chineseplayer.swf&playerskin=http://video2.channelnewsasia.com/cnavideos/playerxin.swf&"'

can someone help

rachel_2390@yahoo.com

Dec. 24, 2008fredy

hello, i'm newbie. how the way to install ffmpeg in window? thx

Dec. 24, 2008kLink

@fredy,

Obtain ffmpeg from: http://sourceforge.net/projects/ffmpeg/

Unzip the package in the same directory as your video files.

Search these forums for the command lines for various uses of ffmpeg (transcoding, image extraction, muxing, streaming on demand).

Mar. 29, 2009Bilal

The script klink provided is not working, can any one help me in this regard?

Mar. 29, 2009lefTy

@Bilal,

Have you uncommented the various debug statements, one-at-a-time, working your way down from the top of the script, to see what is failing?

Call the script from your browser using the examples at the top of the script, until you find the point of failure.

Mar. 29, 2009Bilal

Actually i dont have any file named ffmpeg-10141.exe,

the ffmpeg i downloaded have ffmpeg.exe,

i renamed this file to ffmpeg-10141.exe but still the script isnt working.

Mar. 29, 2009lefTy

The filename doesn't matter, as long as it matches whatever you have, name it alakazamm if you want.

Did you try the debug steps that I posted above?

Jun. 13, 2009windows + linux

<?php
flv_convert_get_thumb('uploads/*.mpeg', 'thumbs/out_thumb.jpg', 'encoded/out_vid.ogm');
// code provided and updated by steve of phpsnaps ! thanks
// accepts:
// 1: the input video file
// 2: path to thumb jpg
// 3: path to transcoded mpeg?
function flv_convert_get_thumb($in, $out_thumb, $out_vid)
{
// get thumbnail
$cmd = 'C:\wamp\www\encoder\ffmpeg -v 0 -y -i '.$in.' -vframes 1 -ss 5 -vcodec mjpeg -f rawvideo -s 286x160 -aspect 16:10 '.$out_thumb;
$res = shell_exec($cmd);
// $res is the output of the command
// transcode video
$cmd = 'C:\wamp\www\encoder\mencoder '.$in.' -o '.$out_vid.' -af volume=10 -aspect 16:9 -of avi -noodml -ovc x264 -x264encopts bitrate=500:level_idc=41:bframes=3:frameref=2: nopsnr: nossim: pass=1: threads=auto -oac mp3lame';
$res = shell_exec($cmd);
}

?>

Jun. 17, 2009for ubuntu 8.10

<?phpflv_convert_get_thumb('input.avi', 'output.jpg', 'output.ogm');// code provided and updated by steve of phpsnaps ! thanks// accepts:// 1: the input video file// 2: path to thumb jpg// 3: path to transcoded mpeg?function flv_convert_get_thumb($in, $out_thumb, $out_vid){ // get thumbnail $cmd = 'ffmpeg -v 0 -y -i '.$in.' -vframes 1 -ss 5 -vcodec mjpeg -f rawvideo -s 286x160 -aspect 16:9 '.$out_thumb; $res = shell_exec($cmd); // $res is the output of the command // transcode video $cmd = 'mencoder '.$in.' -o '.$out_vid.' -af volume=10 -aspect 16:9 -of avi -noodml -ovc x264 -x264encopts bitrate=500:level_idc=41:bframes=3:frameref=2: nopsnr: nossim: pass=1: threads=auto -oac mp3lame'; $res = shell_exec($cmd);}?>


Line, 3 is where you input your video, the output.jpg is the output mpeg also called jpg image format which will be be grabbed. the output.ogm is the actual encoded video. ogm is just a open source video container like avi mkv etc. you can change it to whatever suits your needs.

Line 12, deals with the image grabbing which uses ffmpeg, not much to be said here.


Line 16, deals with Mencoder this basically is the encoding engine, here you can change bit-rate, video width and height and aspect ratio. and volume control.


Dependency packages and operating systems

1. Ubuntu 8.10 Desktop or Server version
2. Lamp server - php5 CLI everything that includes inside Lamp server
3. FFmpeg
4. Mencoder

thanks it.

Aug. 04, 2009Venkat

isit possible to convert an swf to flv or avi or mpg....
if so how to,,!
could u please tell me how....


Thanks in advance,
Venkat.

Sep. 29, 2009Raja

Its Not possible Machi

Add a reaction

You can also return to the category or try this search for related threads.


 

Search the Forums

Go

Support

Support Here are some helpful links to learn more about the JW Player™:

Monetize Your Video

Monetize Your Video Earn money with ads from LongTail's AdSolution. Watch our demos and sign up now!

Why Buy a License?

Why Buy a License? If you don’t buy a commercial license, you cannot use a JW Player™ on (i) a site that has ads; (ii) a corporate site; or a (iii) CMS. Our licenses are very inexpensive, so what are you waiting for? Buy a license today.