Monday, April 1, 2013

Fixing MP4 not playing until full video loads in browser

I had a problem where my mp4 videos were not played by video.js player until the video was fully downloaded by the browser. after some searching I found out it was because mp4 file metadata ( moov atom) is on the end of the file and the player cannot locate it for streaming when initially played.

I was saved by this small php library http://code.google.com/p/moovrelocator/
This will read a video and move MOOV-Atom to the front of the mp4 file.

there is a demo in _demo folder. if you get errors finding the paths, try giving direct paths like this in index.php

$inFile = '../../videos/'.$tempv;
// output (processed file)
$outFile = '../../new_videos/'.$tempv;

if you get zend errors try giving direct paths in Moovrelocator.class.php


// atom poc class
require_once 'atom/Atom.class.php';

// bytearray to operate direct on bytes in memory just to simulate Adobe's AS3 Bytearray
require_once 'bytes/Bytearray.class.php';

// transform (taken from php-reader)
require_once 'bytes/Transform.class.php';

you may also want to change your php.ini max execute time and memory use if you are looping through lot of videos in a folder.