Tech Site & Blog Blog about technology & programming

7Oct/096

Convert video to FLV with PHP

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

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.

Installing FFmpeg

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:
apt-get install ffmpeg

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:
apt-get install ffmpeg-php

Now you should be able to use it.

Example PHP script:
<?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>

This script saves an uploaded file, converts it to FLV and makes a screenshot of the movie at 5th second.
Tagged as: 6 comments
Comments (6)
  1. Hello from Russia!
    Can I quote a post in your blog with the link to you?

  2. Hello!

    Yes, you can quote it.

  3. hi,
    there any way to get quality or vimeo hq
    thanks,
    eduardo

  4. Hi there!
    I have used your php script ... there is a syntax error at line 14 ...something about last ')' ....

  5. how can in install ffmpeg in php. Please help me i am new to use this.


Leave a comment