MP3 Processing File System

From GM-RKB
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

  • Example

# Example of using TinyTag to extract metadata from an MP3 file
import tinytag
from tinytag import TinyTag
file_path = "file.mp3"  # Define the path to the MP3 file
def seconds_to_hms(seconds):
   """
   Convert seconds to hours:minutes:seconds format.
   """
   hours = int(seconds // 3600)
   minutes = int((seconds % 3600) // 60)
   seconds = int(seconds % 60)
   return f"{hours:02d}:{minutes:02d}:{seconds:02d}"
# Extract metadata using TinyTag
tag = TinyTag.get(file_path)
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}")
# This demonstrates how to use TinyTag to get MP3 metadata and convert the duration from seconds to a readable format.
# Example of splitting an MP3 file into chunks with PyDub, including overlap
from pydub import AudioSegment
import os
def split_mp3_with_overlap(file_path, num_chunks, overlap_duration):
   """
   Splits an MP3 file into specified number of chunks with an overlap duration in minutes.
   """
   audio = AudioSegment.from_mp3(file_path)  # Load the MP3 file
   total_length = len(audio)  # Total audio length in milliseconds
   #
   # Convert milliseconds to hours, minutes, and seconds for total duration
   total_length_hr, total_length_min, total_length_sec = convert_ms_to_time(total_length)
   print(f"Total duration: {total_length_hr}h {total_length_min}m {total_length_sec}s")
   #
   overlap_ms = overlap_duration * 60 * 1000  # Convert overlap duration to milliseconds
   adjusted_total_length = total_length - overlap_ms * (num_chunks - 1)  # Adjust length to account for overlaps
   chunk_duration = adjusted_total_length / num_chunks  # Duration of each chunk
   #
   base_name = os.path.splitext(os.path.basename(file_path))[0]  # Get base file name without extension
   #
   for i in range(num_chunks):
       start_time = i * chunk_duration + min(i * overlap_ms, total_length)
       end_time = start_time + chunk_duration + overlap_ms
       end_time = min(end_time, total_length)  # Ensure end time does not exceed total length
       chunk = audio[start_time:end_time]  # Extract audio chunk
       chunk.export(f"{base_name}_chunk_{i+1}.mp3", format="mp3")  # Export chunk as 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)
# Example usage to split an MP3 file into 3 chunks with 1 minute of overlap
split_mp3_with_overlap(file_path, num_chunks=3, overlap_duration=1)