MP3 Processing File System

From GM-RKB
Revision as of 18:10, 6 March 2024 by Gmelli (talk | contribs) (Created page with "An MP3 File Processing System is a file processing system that performs MP3 file processing operations (on MP3 files). * <B>Context:</B> ** It can (typically) perform operations such as converting audio files to and from MP3 format, editing MP3 files (e.g., trimming, merging), analyzing MP3 files for properties like bit rate and frequency, and tagging MP3 files with metadata. ** It can (often) support batch processing of m...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

An MP3 File Processing System is a file processing system that performs MP3 file processing operations (on MP3 files).



References

2023

file_path="file.mp3"

def seconds_to_hms(seconds):

   hours = int(seconds // 3600)
   minutes = int((seconds % 3600) // 60)
   seconds = int(seconds % 60)
   return f"{hours:02d}:{minutes:02d}:{seconds:02d}"

import tinytag from tinytag import TinyTag

  1. Get the tag of the file

tag = TinyTag.get(file_path)

  1. Print out metadata

duration_hms = seconds_to_hms(tag.duration) print(f"Title: {tag.title}") print(f"Artist: {tag.artist}") print(f"Album: {tag.album}") print(f"Year: {tag.year}") print(f"Track Number: {tag.track}") print(f"Genre: {tag.genre}") print(f"Duration: {duration_hms}")

from pydub import AudioSegment import os

def split_mp3_with_overlap(file_path, num_chunks, overlap_duration):

   # Load the MP3 file
   audio = AudioSegment.from_mp3(file_path)
   # Calculate total length of the audio in milliseconds
   total_length = len(audio)
   
   # Convert total length to hours, minutes, and seconds
   total_length_hr, total_length_min, total_length_sec = convert_ms_to_time(total_length)
   # Debug: Report total duration of the MP3 file
   print(f"Total duration of the file: {total_length_hr} hours, {total_length_min} minutes, {total_length_sec} seconds")
   # Convert overlap duration from minutes to milliseconds
   overlap_ms = overlap_duration * 60 * 1000
   # Adjust total length to exclude overlaps from the calculation
   adjusted_total_length = total_length - overlap_ms * (num_chunks - 1)
   # Ensure the adjusted total length is not less than 0
   adjusted_total_length = max(adjusted_total_length, 0)
   # Calculate the duration of each chunk without considering the overlap for the first calculation
   chunk_duration = adjusted_total_length / num_chunks
   # Extract base file name without extension
   base_name = os.path.splitext(os.path.basename(file_path))[0]
   for i in range(num_chunks):
       # Calculate the start and end times for each chunk considering the overlap only for the start time
       start_time = i * chunk_duration + min(i * overlap_ms, total_length)
       end_time = start_time + chunk_duration + overlap_ms
       # Make sure the end time does not exceed the total length of the audio
       end_time = min(end_time, total_length)
       # Slice the audio for the chunk
       chunk = audio[start_time:end_time]
       # Debug: Report duration of each chunk in a human-readable format
       chunk_duration_min, chunk_duration_sec = divmod((end_time - start_time) / 1000, 60)
       print(f"Chunk {i+1}: Duration = {int(chunk_duration_min)} minutes, {int(chunk_duration_sec)} seconds")
       # Export chunk as MP3 with original file name as prefix and chunk number as suffix
       chunk.export(f"{base_name}_chunk_{i+1}.mp3", format="mp3")

def convert_ms_to_time(duration_ms):

   """
   Convert milliseconds to hours, minutes, and seconds.
   """
   seconds = (duration_ms / 1000) % 60
   minutes = (duration_ms / (1000 * 60)) % 60
   hours = (duration_ms / (1000 * 60 * 60))
   return int(hours), int(minutes), int(seconds)
  1. Example usage:

split_mp3_with_overlap(file_path, num_chunks=3, overlap_duration=1)