Format Video Length in HH:MM:SS from Command Line

By  on  

In my experimentation with audio and video manipulation, I've found that most tools prefer to handle time in HH:MM:SS (hour:minute:second) format.  I always presumed that using seconds would be easier but I'm barely a novice media tool user, much less a tool creator.

When I wrote the Create Short Preview from Video post, I calculated the video length in seconds with the following command:

length=$(ffprobe $sourcefile  -show_format 2>&1 | sed -n 's/duration=//p' | awk '{print int($0)}')]

That was helpful in detecting if a video was long enough to generate a preview for, but I then needed to get that length in HH:MM:SS format:

formattedlength=$(printf "%02d:%02d:%02d\n" $(($length/3600)) $(($length%3600/60)) $(($length%60)))

In the end you need to evaluate loads of individual statements to get your final HH:MM:SS format!

Recent Features

Incredible Demos

  • By
    Create a Simple News Scroller Using MooTools, Part I:  The Basics

    News scroller have been around forever on the internet. Why? Because they're usually classy and effective. Over the next few weeks, we'll be taking a simple scroller and making it into a flexible, portable class. We have to crawl before we...

  • By
    Animated AJAX Record Deletion Using Dojo

    I'm a huge fan of WordPress' method of individual article deletion. You click the delete link, the menu item animates red, and the item disappears. Here's how to achieve that functionality with Dojo JavaScript. The PHP - Content & Header The following snippet goes at the...

Discussion

    Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!