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

  • By
    CSS @supports

    Feature detection via JavaScript is a client side best practice and for all the right reasons, but unfortunately that same functionality hasn't been available within CSS.  What we end up doing is repeating the same properties multiple times with each browser prefix.  Yuck.  Another thing we...

  • By
    How to Create a RetroPie on Raspberry Pi – Graphical Guide

    Today we get to play amazing games on our super powered game consoles, PCs, VR headsets, and even mobile devices.  While I enjoy playing new games these days, I do long for the retro gaming systems I had when I was a kid: the original Nintendo...

Incredible Demos

  • By
    Drag & Drop Elements to the Trash with MooTools 1.2

    Everyone loves dragging garbage files from their desktop into their trash can. There's a certain amount of irony in doing something on your computer that you also do in real life. It's also a quick way to get rid of things. That's...

  • By
    The Simple Intro to SVG Animation

    This article serves as a first step toward mastering SVG element animation. Included within are links to key resources for diving deeper, so bookmark this page and refer back to it throughout your journey toward SVG mastery. An SVG element is a special type of DOM element...

Discussion

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