Updated formatting & removed redundancies
This commit is contained in:
+15
-10
@@ -1,4 +1,4 @@
|
||||
#Confirmed working as of 1/22/23
|
||||
'''Align tracks end to end, chop based on chapters list durations, export to final folder, archive originals'''
|
||||
|
||||
import os
|
||||
import shutil
|
||||
@@ -18,19 +18,22 @@ def export_audio_file(start, end, name):
|
||||
author_name = folder.split(" - ")[0].strip()
|
||||
export_folder = os.path.join(destination_path, author_name, folder_name)
|
||||
author_folder = os.path.join(destination_path, author_name)
|
||||
'''If an author folder doesn't already exist it makes one'''
|
||||
if not os.path.exists(author_folder):
|
||||
os.makedirs(author_folder)
|
||||
export_folder = os.path.join(author_folder, folder_name)
|
||||
|
||||
'''Skips the above if an author already exists and adds the book to it'''
|
||||
if not os.path.exists(export_folder):
|
||||
os.makedirs(export_folder)
|
||||
export_path = os.path.join(export_folder, f"{name}.mp3")
|
||||
export.export(export_path, format="mp3")
|
||||
|
||||
'''Defines metadata & artwork files'''
|
||||
metadata_file = os.path.join(folder_path, "cleaned_metadata.json")
|
||||
chapters_file = os.path.join(folder_path, "overdrive_chapters_ms.txt")
|
||||
album_art = os.path.join(folder_path, "folder.jpg")
|
||||
if os.path.exists(album_art):
|
||||
'''Copies metadata & artwork files alongside chapterized audio'''
|
||||
shutil.copy(album_art, export_folder)
|
||||
shutil.copy(metadata_file, export_folder)
|
||||
|
||||
@@ -40,7 +43,7 @@ for folder in sorted(os.listdir(main_directory)):
|
||||
folder_path = os.path.join(main_directory, folder)
|
||||
if os.path.isdir(folder_path):
|
||||
try:
|
||||
# Import MP3s in alphabetical order
|
||||
'''Import MP3s in alphabetical order'''
|
||||
mp3_files = sorted(os.listdir(folder_path))
|
||||
mp3_files = [os.path.join(folder_path, file) for file in mp3_files if file.endswith(".mp3")]
|
||||
|
||||
@@ -49,10 +52,10 @@ for folder in sorted(os.listdir(main_directory)):
|
||||
print(mp3_file)
|
||||
combined += AudioSegment.from_file(mp3_file)
|
||||
|
||||
# Align end to end
|
||||
'''Align end to end'''
|
||||
combined = combined.set_channels(1)
|
||||
|
||||
# Import labels
|
||||
'''Import label names/durations'''
|
||||
labels_file = os.path.join(folder_path, "overdrive_chapters_ms_spans.txt")
|
||||
labels = []
|
||||
with open(labels_file, "r") as f:
|
||||
@@ -60,25 +63,27 @@ for folder in sorted(os.listdir(main_directory)):
|
||||
start, end, name = line.strip().split("\t")
|
||||
labels.append((start, end, name))
|
||||
|
||||
# Initialize counter for duplicate label names
|
||||
'''Initialize counter for duplicate label names'''
|
||||
counter = {}
|
||||
|
||||
# Initialize counter for file export
|
||||
'''Initialize counter for file export'''
|
||||
file_count = 0
|
||||
|
||||
for i, label in enumerate(labels):
|
||||
start, end, name = label
|
||||
'''This ensures that the final end duration matches the length of the audiobook'''
|
||||
if end == "0" and i == len(labels) - 1:
|
||||
end = combined.duration_seconds * 1000
|
||||
|
||||
file_count += 1
|
||||
name = f"{file_count:03}_{name.replace('/', '_')}"
|
||||
# format file_count as 3 digit number
|
||||
'''format file_count as 3 digit number'''
|
||||
export_audio_file(start, end, name)
|
||||
|
||||
|
||||
'''Prints if there's an error'''
|
||||
except Exception as e:
|
||||
print(f"Error processing {folder}: {e}")
|
||||
|
||||
#Archive everything except chapters_list.py
|
||||
'''Archive everything except chapters_list.py'''
|
||||
if os.path.isdir(folder_path) and folder != 'chapters_list.py':
|
||||
shutil.move(folder_path, archive + '/' + folder)
|
||||
Reference in New Issue
Block a user