Advanced Linux tools for improving your website’s media experience
Over the last few years, I've had the opportunity to work on sites that serve a variety of media types. Fortunately, server Linux distributions come "batteries included", and have some great command-line applications to automate handling many formats.
I'm going to briefly describe and show usage of a few different popular media file conversion programs. Hopefully, this post will give you some ideas about what's within grasp for your web media projects, and maybe give you some inspiration for your next content-heavy web app. All of these apps are easily available from Ubuntu's package repository, just one apt-get
away!
Images: ImageMagick
This is one of the more well known image manipulation tools on Linux. For example, for a nicely tweaked 150×150 thumbnail, which will trim away single blank borders and keep the content centered:
convert IN_FILE.JPG -thumbnail 150^150 -gravity center -extent 150^150 -trim OUT_FILE.JPG
When programming in Python, it is often better to use something with native language bindings. However, there are advanced features unique to Image Magick, such as seam carving, an algorithm for "content aware image resizing", which allows changing the proportions of an image without distorting the foreground.
Videos: libav
Libav is a powerful video conversion library, which also supports adding subtitles, overlaying images, extracting images, dubbing audio, and much more. It supports quite a few image formats out of the box, making it an ideal choice for encoding HTML5 video, or transcoding video from uploaded formats. To convert between formats, resizing to 240 by 240:
avconv -i in_file.mp4 -s 240x240 out.webm
E-books: Calibre
Calibre is an open source e-book reader and organizing suite written in Python. More importantly for developers, it exposes its extremely powerful e-book conversion system on the command line, allowing you to easily convert from HTML to AZW (Amazon's Kindle) or ePub (Barnes and Noble Nook), and many others, or write extensions to support new input or output formats. It even has a large number of optimization presets, allowing you to target specific e-reader hardware.
Create an ePub from an HTML file, including a few meta data parameters:
ebook-convert source.html output.epub --title="Adventures of Tom Sawyer" --authors="Mark Twain"
HTML: PhantomJS
PhantomJS runs an in-memory version of the WebKit engine to render webpages without ever using the screen or display server. It is often used for test suites for browser JavaScript, but can also be used for taking extremely accurate screenshots or thumbnails of any HTML page, exporting them to PNG or PDF formats.
You might like this little PhantomJS script I wrote some time ago that can help with rendering HTML. You can use my script to render a cropped portion of a website with:
phantomjs rasterize.js '{"input": "/path/to/file.html", "output": "output.png"}'
Note arguments are in JSON, to make interfacing with another application slightly easier (and the code simpler).
3D Files: Meshlab
With the rising interest of 3D printing and rapid hardware prototyping, conversion and visualization of 3D formats is becoming an increasingly important aspect of media support—even GitHub now supports 3D previews! Fortunately, there are Linux tools to make this easier. Meshlab is a popular mesh editing program. It also sports a nice command-line version called meshlabserver that allows conversion between quite a few different model formats: obj, off, 3DS, ptx, ply, dae.
To convert a 3D model from 3DS into the STL format, a popular format for 3D printers:
meshlabserver -i designed_object.3ds -o printer_ready_output.stl
Once you've converted your input formats, you might like to check out a ray-tracer such as POV-Ray for rendering that 3D file into a high quality preview or thumbnail of the 3D file.