Christian Heilmann

Making vid.ly conversion and embedding easy

I am lucky enough to have a vid.ly pro account to convert videos. Lucky because lately the free service started limiting the amount of times you can watch a video in a month (as they were hammered by a lot of traffic from Asia abusing the service). In case you still haven’t heard about vid.ly – it is a service that converts a video into a few dozen formats for HTML5 embedding and gives you a single URL to redirect devices to the correct format of the video.

Now, to make it easier for my colleagues to convert and embed videos in HTML5, I built a simple interface for converting and embedding a video on our blogs. For this I am using the API, but I wanted to avoid having to give my key out for colleagues to use.

The interface to convert videos is pretty easy:


Vid.ly conversion and embed



Simply add the URL of the video to convert below and you get the embed code.
An email will inform you about the successful conversion.
Conversion could take up to an hour.







One of the cool features of the API is that it allows you to define an email that is not the one connected with the key to be the one that gets notified both of the conversion start, errors and success email. That made my job a lot easier. All I needed to do was assemble the correct XML and send it to the API. As the result is XML, too, I needed to check what came back and give feedback in the form:


$key = ‘{add your key here}’;
$message = ‘’;
if(isset($_POST[‘send’])){

if($_POST[‘email’] !== ‘’ && $_POST[‘url’] !== ‘’) {
$query = ‘‘.
‘AddMedia481‘.
‘‘.$key.’‘.
‘‘.$_POST[‘email’].’‘.
‘‘;
$url = ‘http://m.vid.ly/api/’;
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS,’xml=’.urlencode($query));
$result = curl_exec($ch);
curl_close($ch);

$xml = simplexml_load_string($result);

if($xml->Success) {
$vid = $xml->Success->MediaShortLink->ShortLink;
$video = ‘ ’ poster=”http://cf.cdn.vid.ly/’.$vid.’/poster.jpg”>‘.
‘ ‘type=”video/mp4”>‘.
‘ ‘type=”video/webm”>‘.
‘ ‘type=”video/ogg”>‘.
‘.
‘ ‘width=”500”>
‘.
‘‘;
$message = ‘

Conversion started

‘.

The video conversion is under way. ‘.
‘You should get an email telling you so and an email when ‘.
‘the video URL is ready. The code to copy & paste into ‘.
‘the blog is:

‘.
‘;
} else {
$message = ‘

Error

‘.

Something went wrong in the conversion,’.
‘please try again.

‘;
}

} else {
$message = ‘

Error

‘.

Please provide a video URL and email

‘;
}

}
?>

Pretty simple, isn’t it. Now my colleagues can add their email, give the form a URL where the video to convert is on the web and will get a copy and paste HTML for the video, for example:


poster=”http://cf.cdn.vid.ly/1l5i5m/poster.jpg”>






Which results in:

Giving HTML5 video to the browsers who support it and a link to vid.ly for those who don’t :) The code is on GitHub as a Gist: