How to measure mp3 file playback time with python

There are two ways to get the duration of an mp3 file. One is to use mutagen and the other is to use pysox. Each method has its strengths and weaknesses.

The mutagen method is lighter and faster than the pysox method.

That's why mutagen only checks ID3 tags in mp3 files. On the other hand, pysox seems to use the Sox CLI to check the binaries of mp3 files.

The pysox method can determine the duration without using ID3 information, that is, it can recognize files with invalid ID3 information or files without ID3 information. By using pysox, you can check the duration without using ID3 information, that is, you can detect the duration of files with invalid ID3 information or files without ID3 information.

How to use mutagen

from mutagen.mp3 import MP3

def mutagen_length(path):
    try:
        audio = MP3(path)
        length = audio.info.length
        return length
    except:
        return None

length = mutagen_length(wav_path)
print("duration sec: " + str(length))
print("duration min: " + str(int(length/60)) + ':' + str(int(length%60)))

How to use pysox

Note: pysox needs SOX cli.

import sox

def sox_length(path):
    try:
        length = sox.file_info.duration(path)
        return length
    except:
        return None

length = sox_length(mp3_path)

print("duration sec: " + str(length))
print("duration min: " + str(int(length/60)) + ':' + str(int(length%60)))

Recommended Posts

How to measure mp3 file playback time with python
How to measure execution time with Python Part 1
How to measure execution time with Python Part 2
How to read a CSV file with Python 2/3
[Python] How to read excel file with pandas
How to measure processing time in Python or Java
How to convert JSON file to CSV file with Python Pandas
[Python] How to FFT mp3 data
Python: How to use async with
[Python] Write to csv file with Python
Output to csv file with Python
MP3 to WAV conversion with Python
How to get started with Python
How to use FTP with Python
Python: How to use pydub (playback)
How to calculate date with python
[Python Kivy] How to create an exe file with pyinstaller
How to read an Excel file (.xlsx) with Pandas [Python]
[Python] How to convert db file to csv
How to convert Python to an exe file
How to work with BigQuery in Python
How to do portmanteau test with python
How to display python Japanese with lolipop
How to enter Japanese with Python curses
[Python] How to deal with module errors
How to write offline real time Solve F01 problems with Python
[ROS2] How to play a bag file with python format launch
How to install python3 with docker centos
How to calculate "xx time" in one shot with Python timedelta
How to get the date and time difference in seconds with python
How to write offline real time I tried to solve E12 with python
How to upload with Heroku, Flask, Python, Git (4)
How to enjoy programming with Minecraft (Ruby, Python)
[REAPER] How to play with Reascript in Python
How to do multi-core parallel processing with python
Strategy on how to monetize with Python Java
How to achieve time wait processing with wxpython
How to create a JSON file in Python
[Python] How to draw multiple graphs with Matplotlib
Convert svg file to png / ico with Python
How to crop an image with Python + OpenCV
How to specify attributes with Mock of python
How to use tkinter with python in pyenv
[Python] How to handle Japanese characters with openCV
I want to write to a file with Python
[Python] How to compare datetime with timezone added
How to install Python
How to install python
How to drop Google Docs in one folder in a .txt file with python
High resolution acoustic signal processing (1) --How to read 24-bit wav file with Python
How to convert / restore a string with [] in python
How to add help to HDA (with Python script bonus)
[Python] How to draw a line graph with Matplotlib
How to scrape image data from flickr with python
How to do hash calculation with salt in Python
[Introduction to Python] How to iterate with the range function?
Explain in detail how to make sounds with python
I tried to touch the CSV file with Python
How to upload with Heroku, Flask, Python, Git (Part 3)
How to run tests in bulk with Python unittest
[Python] How to specify the download location with youtube-dl