/labs

This is a blog by the staff at the web agency Earth People in Stockholm Sweden. Check out our work and get in contact with us here.

automated screendump creation

2011-04-12 10:53:33

i recently had to create jpg thumbs of the first frame from a bunch of videos. doing this manually is not really an option if you have 100 files or more, so commandline to the rescue. i had an ubuntu box, on which i installed ffmpeg:

sudo apt-get install ffmpeg

then cd into the directory containing your video files and run this oneliner:

ls | xargs -i ffmpeg -i {} -vcodec mjpeg -vframes 1 -an -f rawvideo -s 114x64 {}.jpg

in the same directory, i now have a jpg screendump of each video file.
ffmpeg can decode a bunch of formats, should you need support for some other weird format you may need to compile your own version of ffmpeg. good luck!

/ peder fjällström

html5 validation with(!) facebook opengraph

2010-09-17 22:27:22

html5 is that new, cool(?) technology which you should(?) be using when making new sites. but, being the thorough developer you are – it just eats you up from the inside when your brand new html5 website won’t validate because your client decided to add a facebook like button.

the facebook open graph protocol/namespace/api/whatever which was released in spring 2010 will only validate with an xhtml doctype. if you want to work in a html5 doctype and use opengraph, you need to apply a little bit of server logic. have a look at this, i won’t bother explaining the source code, i think you’ll get it.

<?php
function is_facebook(){
if(!(stristr($_SERVER["HTTP_USER_AGENT"],’facebook’) === FALSE))
return true;
}
}
?><!DOCTYPE html>
<html dir=”ltr” lang=”en-US”<?php if(is_facebook()){echo ‘ xmlns:fb=”http://www.facebook.com/2008/fbml” xmlns:og=”http://opengraphprotocol.org/schema/”‘;}?>>
<head>
<title><?php bloginfo(‘name’); ?></title>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″ />

<?php if(is_facebook()){?>
<meta property=”og:title” content=”<?php echo $title;?>”/>
<meta property=”og:description” content=”<?php echo $description;?>”/>
<meta property=”og:type” content=”article”/>
<meta property=”og:image” content=”<?=$path_to_page_thumbnail?>”/>
<meta property=”og:url” content=”http://<?php echo $_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"];?>”/>
<meta property=”og:site_name” content=”<?=$the_name_of_the_site?>”/>
<meta property=”fb:appid” content=”<?=$your_fb_app_id?>”/>
<?php }?>

one thing to have in mind is that page level caching is a no go if you apply this tactic. if your cache is clever, you could possibly make an exception for the facebook user agent, delivering fresh content to facebook on all requests.

oh well, it’s just a thought. let me know if i missed something.

/ peder fjällström

Getting stats from the Facebook Like button your way

2010-09-05 21:28:48

So you’ve implemented the Facebook Like button, and you’d like to know – programmatically – what your users Like on your site. This is a bit of a pain, since there’s nothing in the Facebook Open Graph for this. There’s some stuff in the insights api, but it’s not really usable if you’d like to do anything fun with your data.

I’ve written a PHP class which you can download and use anyway you see fit. It’s kind of simple, but will get the job done. You probably want to add some caching to it, since the old REST-api we’re querying isn’t very responsive.

Use it like this:

# create new instance of like class
$FbLikes = new FbLikes();

# add all the urls you want to measure
$FbLikes->addUrl('http://earthpeople.se/labs/');
$FbLikes->addUrl('http://wplove.se/kom-pa-wpbar/');
$FbLikes->addUrl('http://debaser.se/');

# set the sort order, either 'likes' or 'untouched'
$FbLikes->order_by = 'likes';

# get all the fb like data
$likes = $FbLikes->getLikes();

# echo the results
if($likes){
  foreach($likes as $row){
    echo $row->normalized_url . ': ' . $row->like_count . " facebook likes\n";
  }
}

Enjoy – or fork!

/ peder fjällström

php class for ffmpeg

2010-04-03 18:10:16

i’ve had a few projects over the last years where i’ve needed to convert some video file to flv, generate a thumbnail and get the video’s duration. i’ve never really got around to wrapping this into something reusable. but here it is, get it if you need it.

you’ll also need a non-windows server with ffmpeg installed, and if you need compability with any non-standard formats (3gp/mp3/etc) you need to install these as well. i won”t go into detail about how this is done, just google it.

when ffmpeg is correctly installed on your box, use this class like this:

$encoder = new videoencoder();
$thumbnail = $encoder->export_thumb("yourfile.mp4");
$duration = $encoder->get_duration("yourfile.mp4");
$videofile = $encoder->export_video("yourfile.mp4");

ok i know it’s better to use a service for this, like the excellent encoding.com – but hey, this is more fun – and really fast! also, worth noting is that while ffmpeg happily converts your files to flv, it will use the old “spark” codec for flash videos, instead of the newer and slightly better codec “on2″ due to licensing issues. you can however use mp4 files with recent versions of flash player, which ffmpeg can convert to. use mp4 instead if you worry about video quality (which i guess you should…).

download my php class here.

/ peder fjällström

search for artist from iTunes

2010-03-16 15:57:24

So, say you got this one great track in iTunes, and you want to look for more music from the same artist. Here’s a simple little script that can ease things up a bit.

Download it

This script makes a Amazon search in Firefox for the artist, but as you’re guessing you can make it go to what-ever site you like and search for content. Open the script and replace the url with a new url and save. You can also make it use Safari instead of Firefox.

To install it, place it in your home folder/Library/iTunes/Scripts
If that folder doesn’t exist – make a new one and call it Scripts.
Quit iTunes, restart and an applescript icon should appear in the iTunes menu bar, with your script ready to be clicked on.

/ fredrik mjelle