Mass converting Movie files to FLV

aidanlister

Joined: 2005-12-07
Posts: 56
Posted: Wed, 2007-04-11 04:44

I've spent some time writing a script to convert all the MovieItem files in a gallery to FLV files.

The code looks for a file (in the same directory as it) called "filelist.txt", which is a list of filenames of the movie files you wish to convert (relative to the base of your albums directory). E.g:

album1/foo/file.mov
album2/foo/file.avi

The script handles the conversion, backs up the movie file, and does all the operations needed for gallery to become aware of the changes.

Steps for use:
1) Create your filelist.txt
2) Place in your gallery root folder, make sure debug mode is enabled ($debug = true below), and run via your browser.
3) If everything looks good, set $debug = false and $onlyconvert = true, fire up a terminal and run "php flv_convert_script.php". This will take a while and assumes you have ffmpeg-with-lame set up correctly. You should get a progress bar from ffmpeg.
4) If everything worked, set $debug = false, $onlyconvert = false and run the script with your browser again. You should now confirm that the process worked correctly by viewing the FLV file through your gallery - note the template/database cache will need to be emptied first.
5) If you're sure everything has gone according to plan, set $debug = false and $cleanup = true to delete all the .bak files, and you're done!

Good luck.

<?php
// Written by Aidan Lister <aidan@php.net>
// Please be careful, you can do damage to your gallery with this script

// Config
$debug = true;
$convert = false;
$onlyconvert = false;
$systemrename = false;
$cleanup = false;

// Show errors
ini_set('display_errors', 'on');
error_reporting(E_ALL);

// Load the Gallery API
require_once(dirname(__FILE__) . '/embed.php');
$ret = GalleryEmbed::init(array('fullInit' => true));
if ($ret) {
    echo $ret->getAsText();
    exit;
}
$storage = $gallery->getStorage();

// Get the path to albums
$albumdir = $gallery->getConfig('data.gallery.albums');

// No time out
$gallery->guaranteeTimeLimit(120);

// Iterate all the files in our list
$files = file('filelist.txt');
foreach ($files as $file) {

    // Get all the path names
    $relpath = trim($file);
    $relflvpath = substr($relpath, 0, strlen($relpath) - 3) . "FLV";

    $filepath = $albumdir . $relpath;
    $flvpath = substr($filepath, 0, strlen($filepath) - 3) . "FLV";

    $filename = explode(DIRECTORY_SEPARATOR, $filepath);
    $filename = end($filename);

    $flvname = explode(DIRECTORY_SEPARATOR, $flvpath);
    $flvname = end($flvname);

    // Delete all the .bak files if we're done
    if ($cleanup === true) {
        unlink($filepath . '.bak');
        echo "{$filepath}.bak removed ... <br />";

        // Only do this step
        continue;
    }


    // 1. Convert to FLV in same directory
    // This step should be run in a separte file with the PHP CLI
    $command = "ffmpeg -i $filepath -f flv -vcodec flv -acodec mp3 -ab 64 -ar 22050 -s 640x480 -deinterlace $flvpath";
    if ($debug === true || $convert === false) {
        echo "system($command)<br />";
    } else {
        system($command);
    }
    if ($onlyconvert === true) {
        continue;
    }


    // 2. Replace movie file with derivative
    if ($debug === true || $systemrename === false) {
        echo "rename($filepath, {$filepath}.bak)<br />";
        echo "rename($flvpath, $filepath)<br />";
    } else {
        rename($filepath, $filepath . '.bak');
        rename($flvpath, $filepath);
    }

    // Find the item and grab some locks
    if ($debug !== true) {
        list ($ret, $id) = GalleryCoreApi::fetchItemIdByPath($relpath);
        if ($ret) {
            echo $ret->getAsText();
            exit;
        }

        list ($ret, $item) = GalleryCoreApi::loadEntitiesById($id);
        if ($ret) {
            echo $ret->getAsText();
            exit;
        }

        // Get a lock
        list ($ret, $lockId) = GalleryCoreApi::acquireWriteLock($item->id);
        if ($ret) {
            echo $ret->getAsText();
            exit;
        }
    }

    // 3. Rename in gallery
    if ($debug === true) {
        echo '$item->rename(' . $flvname . ')<br />';
    } else {
        $ret = $item->rename($flvname);
        if ($ret) {
            echo $ret->getAsText();
            exit;
        }
    }

    if ($debug !== true) {
        
        // 4. Rescan
        // Set mime
        $item->setMimeType('video/x-flv');
        
        // Set renderer
        // The API command does nothing, so do it manuaully
        //$item->setRenderer('FlashVideoRenderer');
        $query = 'UPDATE [GalleryItem] SET [GalleryItem::renderer] = "FlashVideoRenderer" WHERE g_id = ?';
        $ret = $storage->execute($query, array($id));
        if ($ret) {
            echo $ret->getAsText();
            exit;
        }
        
        /*
        // This does nothing but cause OBSELETE_DATA errors
        $ret = $item->rescan();
        if ($ret) {
            echo $ret->getAsText();
            exit;
        }
        */

        // 5. Save
        $ret = $item->save();
        if ($ret) {
            echo $ret->getAsText();
            exit;
        }

        // Release locks
        $ret = GalleryCoreApi::releaseLocks($lockId);
        if ($ret) {
            echo $ret->getAsText();
            exit;
        }
    }

    echo "done ...<br /><br />";
}


// Finish
$ret = GalleryEmbed::done();
if ($ret) {
    echo $ret->getAsText();
    exit;
}

?>
AttachmentSize
convert_flv_script.php_.txt4.14 KB
Login or register to post comments
iverson0881

Joined: 2004-08-31
Posts: 8
Posted: Sun, 2007-04-15 12:11

The script appears to work but I believe the current ffmpeg version I'm using cannot convert the files I'm trying to convert.

This appears as my error when trying to run the ffmpeg command manually.

Stream #0.0: Video: flv, yuv420p, 640x480, 25.00 fps, q=2-31, 200 kb/s
Stream #0.1: Audio: 0x0000, 22050 Hz, mono, 64 kb/s
Stream mapping:
Stream #0.1 -> #0.0
Stream #0.0 -> #0.1
Unsupported codec for output stream #0.1

Also I can't run the program, I have to do it as root because it can't create the file. Is this related to your script or a chmod issue?

Much thanks to your efforts :)

Login or register to post comments
aidanlister

Joined: 2005-12-07
Posts: 56
Posted: Fri, 2007-04-20 07:17

It looks like you don't have LAME support enabled. You'll need to recompile ffmpeg to include the lame library - there's plenty of help on google if you're running a standard setup, if you're not it is quite tricky unfortunately.

Re the chmod, that's the way your folder permissions are set up. You can use /lib/support/ to change it to group writeable.

Login or register to post comments
edwardotis

Joined: 2004-07-26
Posts: 38
Posted: Tue, 2008-05-20 22:13

Hi Aidan,

Thanks for the script.

It looks like the variable $convert, needs to be set to true though it is always set to false in the above example. Is that correct?

Also, I get this error when I run the 3rd step, GalleryDataItem::rename, inside the browser or in command line.

rename(/home/mywebsite/mywebsite.net_photos_temp_g2data/albums/first/MVI_0411.AVI, /home/mywebsite/mywebsite.net_photos_temp_g2data/albums/first/MVI_0411.AVI.bak)
rename(/home/mywebsite/mywebsite.net_photos_temp_g2data/albums/first/MVI_0411.FLV, /home/mywebsite/mywebsite.net_photos_temp_g2data/albums/first/MVI_0411.AVI)
Error (ERROR_COLLISION)in modules/core/classes/GalleryDataItem.class at line 333 (GalleryCoreApi::error) in convert2flv.php at line 106 (GalleryDataItem::rename)

Any ideas?

Thanks,

Ed

// Config
$debug = false;
$convert = true;
$onlyconvert = false;
$systemrename = false;
$cleanup = false;

Last Run Details:

Gallery version = 2.2.4 core 1.2.0.6
PHP version = 5.2.3 cgi-fcgi
Webserver = Apache/2.0.61 (Unix) PHP/4.4.7 mod_ssl/2.0.61 OpenSSL/0.9.7e mod_fastcgi/2.4.2 DAV/2 SVN/1.4.2
Database = mysqli 5.0.24a-standard-log, lock.system=database
Toolkits = ArchiveUpload, Exif, Ffmpeg, Getid3, LinkItemToolkit, Thumbnail, Gd, ImageMagick
Acceleration = none, none
Operating system = Linux cadillac 2.4.32-grsec+f6b+gr217+nfs+a32+fuse23+tg+++opt+c8+gr2b-v6.194 #1 SMP Tue Jun 6 15:52:09 PDT 2006 i686
Default theme = matrix
gettext = enabled
Locale = en_US
Browser = Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_2; en-us) AppleWebKit/525.18 (KHTML, like Gecko) Version/3.1.1 Safari/525.18
Rows in GalleryAccessMap table = 21
Rows in GalleryAccessSubscriberMap table = 8
Rows in GalleryUser table = 2
Rows in GalleryItem table = 7
Rows in GalleryAlbumItem table = 2
Rows in GalleryCacheMap table = 0

Login or register to post comments
edwardotis

Joined: 2004-07-26
Posts: 38
Posted: Tue, 2008-05-20 21:29

Hi again,

Does it matter whether I use php4 or php5 to run this script?
Also, is there any difference running it through the browser, or can I just use the shell for all of it?
It looks like the the browser just formats the output of the script.

Thanks!

Ed

Login or register to post comments