Skip to content

Instantly share code, notes, and snippets.

@passy
Created November 30, 2023 11:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save passy/d197ec798fb9cbeaa45d4c1046de9ffe to your computer and use it in GitHub Desktop.
Save passy/d197ec798fb9cbeaa45d4c1046de9ffe to your computer and use it in GitHub Desktop.
Export mp3 chapter markers into an Adobe-Audition compatible CSV
import subprocess
import re
import csv
import sys
# Function to convert seconds to "MM:SS.sss" format
def format_time(seconds):
minutes = int(seconds // 60)
seconds %= 60
return "{:02d}:{:06.3f}".format(minutes, seconds)
# Check if the correct arguments are provided
if len(sys.argv) != 3:
print("Usage: python script.py <input.mp3> <output.csv>")
sys.exit(1)
# Get the input and output file names from the command line arguments
input_file = sys.argv[1]
output_file = sys.argv[2]
# Run the ffmpeg command and get the output
output = subprocess.run(['ffmpeg', '-i', input_file], stderr=subprocess.PIPE, universal_newlines=True)
# Split the output into lines and strip leading and trailing whitespace
lines = [line.strip() for line in output.stderr.split('\n')]
# Open the CSV file for writing
with open(output_file, 'w', newline='') as file:
writer = csv.writer(file, delimiter='\t')
writer.writerow(["Name", "Start", "Duration", "Time Format", "Type", "Description"])
# Loop through the lines
for i in range(len(lines)):
# If the line starts with "Chapter", it contains the start and end times
if lines[i].startswith('Chapter'):
# Extract the start and end times
times_match = re.search('start (.*), end (.*)', lines[i])
title_match = re.search('title : (.*)', lines[i+2])
if times_match and title_match:
times = times_match.groups()
title = title_match.group(1)
# Convert the start time and duration to "MM:SS.sss" format
start_time = format_time(float(times[0]))
duration = format_time(float(times[1]) - float(times[0]))
# Write the name, start time, duration, time format, type, and description to the CSV
writer.writerow([title, start_time, duration, "decimal", "Cue", ""])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment