Export total length of mp3 files in a folder?

10,669

Solution 1

If you're working on Mac OS or any Unix system you can install ffmpeg and use the following command to extract the duration of a single file:

ffmpeg -i filename.mp3 2>&1 | egrep "Duration" | cut -d ' ' -f 4 | sed s/,//

That would for example return "00:08:17.4".

You can use this in a shell script of course, so for example this would list all of the mp3 files in a folder and their duration to the right.

#!/bin/bash
# call me with mp3length.sh directory
# e.g. ./mp3length . 
# or ./mp3length my-mp3-collection

for file in $1/*.mp3
do
    echo -ne $file "\t"
    ffmpeg -i "$file" 2>&1 | egrep "Duration"| cut -d ' ' -f 4 | sed s/,//
done

The following script returns total duration in hours:

#!/bin/bash
# call me with mp3length.sh directory
# e.g. ./mp3length .
# or ./mp3length my-mp3-collection


list-individual-times() {
    for file in $1/*.mp3
    do
        echo -ne $file "\t"
        ffmpeg -i "$file" 2>&1 | egrep "Duration"| cut -d ' ' -f 4 | sed s/,//
    done
}

TOTAL_HOURS=$(list-individual-times $1 | cut -f2 | xargs -I hhmmss date -u -d "jan 1 1970 hhmmss" +%s | awk '{s+=$1}END{print s/3600}')
echo "Total hours: ${TOTAL_HOURS}"

Solution 2

You can get it easily with oneliner:

$ for file in *.mp3; do mp3info -p "%S\n" "$file"; done | paste -sd+ | sed 's+\(.*\)+(\1)/60+' | bc

Explaining:

  • mp3info: gets individual mp3 length in seconds
  • paste: merge column result with + as separator
  • sed: put () around sum and divides by 60 (minutes
  • bc: perform arithmetic operation

Solution 3

You can add a column to the file list in Wİndows Explorer. Add "Length" property colum to folder. Then select all off the mp3 files in the folder. You can see total length in the bottom summary pane of win.exp...

(Not the whole solution to your question but you said "any help would be appreciated :D)

Solution 4

A little late in the day (decade!), but I wanted to do exactly the same thing. Thanks to Windows Powershell, here is a little script that I wrote today.

$path = '~\MS Subbulakshmi\1. Toronto 1977\'
$shell = New-Object -COMObject Shell.Application
$folder = $path
$shellfolder = $shell.Namespace($folder)
$total_duration = [timespan] 0
foreach($file in Get-ChildItem $folder)
{
    $shellfile = $shellfolder.ParseName($file.Name)
    $total_duration = $total_duration + [timespan]$shellfolder.GetDetailsOf($shellfile, 27);
}
Write-Host "Total Duration of $folder is $total_duration"

The output would be something like this

Total Duration of ~\MS Subbulakshmi\1. Toronto 1977\ is 01:52:28

Share:
10,669

Related videos on Youtube

Anastasia
Author by

Anastasia

Updated on September 17, 2022

Comments

  • Anastasia
    Anastasia over 1 year

    I have many folders of mp3 files, and I would like to have a list of the combined duration of all the mp3s in each folder. That would be the ideal solution, but some means of doing a directory print attaching the individual mp3 duration would also be good. Any help would be much appreciated. I am running Windows 7 Home Premium, but have access to a number of other Windows/Mac OS.

    Edit - I actually found an solution using the export function in the freeware program Tagscanner.

    • ChrisF
      ChrisF over 13 years
      I have a WPF application I wrote that will do that (amongst other things), but I haven't uploaded it anywhere yet - still really for internal use only. However, if nothing else come up I could let you have a copy.
    • theggputest55
      theggputest55 about 4 years
      @ChrisF hi can you please give the link to your program that shows mp3 folder length
  • Anastasia
    Anastasia over 13 years
    Thanks, this is pretty much what I do at the moment. The problem is these files are across many hard drives, so I want to be able to export this info to use in spreadsheets and the like without doing it all manually.
  • y. revathi
    y. revathi over 3 years
    put this directly in cmd once you cd to the directory path or what ? this works in what OS. it didnt work for me on win10
  • albfan
    albfan over 3 years
    $foo means bash, so this is *nix OS. On windows you can use MSYS2