Submind YouTube summaries
Thumbnail for BASH Calculate Total MP3 Play Time

BASH Calculate Total MP3 Play Time

Watch on YouTube

Video summary

The video introduces a practical Bash scripting solution designed to calculate the total playback duration of all MP3 files within a specific directory. The presenter, despite feeling unwell, demonstrates how to use the `ffprobe` utility, which is part of the FFmpeg package, to extract precise metadata from media files. By running a command with specific flags like `-v error` and `show_entries=duration`, the script isolates only the necessary duration data in seconds for each file, avoiding the clutter of verbose output that typically accompanies media processing tools. This initial step is crucial because it provides a clean list of durations that can be mathematically processed to find the aggregate time of an entire album or collection. To transform this raw list of individual durations into a single total, the script utilizes the `awk` programming language, which is excellent for formatting and summing numerical data. The core logic involves piping the output from `ffprobe` into `awk`, where the tool iterates through each line to add the duration of the current file to a running sum. Once all files in the directory have been processed, the script formats this total sum using specific placeholders to ensure that hours, minutes, and seconds are displayed with at least two digits (e.g., showing "03" instead of "3"). This formatting step ensures consistency and readability, preventing blank spaces for single-digit hours or missing leading zeros in the final output. The mathematical conversion from a total number of seconds into a readable time format is handled within the `awk` script through modular arithmetic. The presenter explains that to isolate the seconds, the total sum is taken modulo 60, which yields the remainder after dividing by one minute. To find the hours and minutes, the script divides the total sum by 3,600 (the number of seconds in an hour) to get the hour count, and then uses further division and modulo operations to separate the remaining minutes from the leftover seconds. This systematic breakdown allows the script to accurately distribute the total duration into its constituent parts of hours, minutes, and seconds without needing complex external tools. In conclusion, the video presents a complete, reusable Bash script named "MP3 time" that automates this entire process for any directory containing MP3 files. The presenter emphasizes that while the command line sequence appears long and complex at first glance, it is simply a combination of standard utilities like `ffprobe` and `awk` chained together to solve a specific problem. By saving this logic into a script file, users can run it in any folder to instantly see how long their music collection plays in total. The video ends with an invitation for viewers to check the description for a link to download or view the full script, offering a handy tool for musicians and audio enthusiasts who need to track album lengths efficiently.
Read the full video transcript
Hello, and welcome to video from filmsbychris.com. That's Chris with the K. I'm Chris with the K. I'm feeling pretty sick today, but I'm still making you guys a video. Uh, so it's kind of a long command, but I'm going to break it down here. So, I have the script up on the top here. I'm in a folder here. What is this folder? This is a current album I'm working on. I have a a couple of songs in here that I've completed, but I want to know how long my album is so far. So, I want to calculate I want to be able to run and see how long my current album is. So, FF uh, probe is part of the FFmpeg package, uh, and it can get you information from media files. So, again, this is kind of a long command, but I'm just going to take this. I'm going to paste it down here, and then I'm going to give it one of my files. So, I'll give it this MP3 file. And it gives me here how many seconds long that MP3 file is all the way down to the fraction of a second. That's nice. So, I want to be able to go through all those and calculate them out and get the total time in hours, and minutes, and seconds. So, that's what this loop does. So, here we're going to be looping through. It's going to look at all the MP3s in the current directory. And it's going to run this command on it. So, real quick, look at this command. FFprobe. So, again, I just looked up this command, but basically, uh, -v error. If you don't put that, you're going to get a whole bunch of output. Kind of like when you play the, uh, the or you convert something with FFmpeg, it gives you a whole bunch of information. That narrows it down. Here, we're saying show entries. Just show the the the duration. And then, this if you don't put this, there's like tags that it puts around it saying what that that duration is. Uh, you again, I just know that this this works, right? Uh, so again, you can take this command, give it an MP3 file. I'll give it a different MP3 file. And there you go, we have the seconds for that. So, we're going to take that. So, if we were to run this loop again on all the MP3s in here, we get a long list of each file and how long it is. Well, if we were to take that and pipe it into this, what's going on here? Well, this is awk. Awk is great for formatting uh, any input so we're giving it, and it's going to look at the first thing. So, as it's going through each line, it's going to add it to the previous sum. That's going to give us a total. What are we going to do then? Well, then we're going to print F to get the format. We're going to say total time, colon, and then here we are giving it uh we're going to say we want hours, minutes, and seconds. But what we're saying here is this is a digit and we want it to be at least two digits long. So, that's going to give us a um a placeholder zero. So, if I was to run this script here, you can see it takes second, we have 00 here and 03 here. If we didn't say percent D2 or sorry, percent 02D, um that's again saying that each of these should be two digits. So, if I didn't have that, uh it would say three here instead of 03 and the 00 for the hours would just be blank. So, that's what what this is doing here, but it's taking for each of these variables the uh output of these commands here. So, what's going on here? Well, we're taking the sum, so all of these added up, and we're going to modulate that 60. So, what's that saying is uh we're going to basically divide in the remainder. So, that's how we're going to get our seconds. Here we're going to take the total, we're going to modulate it to 3,600. That's how many seconds there are in an hour, and then we're going to divide that by 60 cuz there's 60 seconds in an hour, and then we're going to take the sum, and we're going to divide that by 3,600, and that will get us how many hours there are. So, uh yeah, that's it. It's kind of a long command, but again, you just put it into a script. So, I called mine uh MP3 time. So, I run that in any directory and it will calculate up the total time of the MP3s in that directory. Uh so, there'll be a link in the description to this script. Check it out. Thanks for watching, and as always, I hope that you have a great day.