Dejan Levec

Convert video to FLV with PHP

[b]How to convert video to .flv with php so that it can be played by Flash Player?[/b]

This is a simple approach to converting videos to .flv with PHP. For example, users are allowed to upload videos, and then you can play it by Flash Player.

[b]Installing FFmpeg[/b]

Probably you already have PHP installed, so let\’s continue with FFmpeg. Following commands works only in Debian/Ubuntu distribution.

To install FFmpeg you simply need to type:
[quote]apt-get install ffmpeg[/quote]
FFmpeg is a library for converting video and capturing screenshots from video.

Now you need some sort of a plug in to allow PHP to access ffmpeg. So we need ffmpeg-php, which can be installed by following command:
[quote]apt-get install ffmpeg-php[/quote]
Now you should be able to use it.

[b]Example PHP script:[/b]
[quote]<?php
if($_FILES[\’file\’]) {
if (!$_FILES[“file”][“error”] > 0) {
move_uploaded_file($_FILES[“file”][“tmp_name”], $_FILES[“file”][“name”]);

$movie = new ffmpeg_movie($_FILES[“file”][“name”]);

$srcWidth = $movie->getFrameWidth(); //Movie width
$srcHeight = $movie->getFrameHeight(); //Movie height
$srcFPS = $movie->getFrameRate(); //Movie frame rate
$srcAB = intval($movie->getAudioBitRate()/1000); //Audio bit rate
$srcAR = $movie->getAudioSampleRate(); //Audio sample rate

exec(“/usr/bin/ffmpeg -i “.$_FILES[“file”][“name”]).” -ar 22050 -ab 32 -f flv -s “.$srcWidth.”*”.$srcHeight.” new_movie.flv”);
//Convert a movie from original type to FLV

exec(“/usr/bin/ffmpeg -i new_movie.flv -an -ss 00:00:05 -an -r 1 -vframes 1 -y new_movie%d.jpg “);
//Make screenshot of a movie at 5th second
}
}
?>
<form action=”upload.php” method=”post” enctype=”multipart/form-data”>
File: <input type=”file” name=”file” /> <input type=”submit” value=”Upload” />
</form>[/quote]
This script saves an uploaded file, converts it to FLV and makes a screenshot of the movie at 5th second.

Leave a Reply

Your email address will not be published. Required fields are marked *