cue2txt/cue2txt.py

34 lines
1.4 KiB
Python
Raw Permalink Normal View History

2023-08-15 23:37:39 +02:00
import argparse
import re
# Command arguments
parser = argparse.ArgumentParser(
description="Let's convert a cue sheet file (.cue tracklist) in a text file more comfortably readable by humans.",
)
parser.add_argument("file", type=str, help="path to cue file")
parser.add_argument("-s", "--show", action="store_true", help="show results")
parser.add_argument("-o", "--output", type=str, help="path to output txt file")
args = parser.parse_args()
filepath = args.file
show_state = args.show
if args.output != None: # output filepath is optional
new_filepath = args.output
else:
new_filepath = re.findall(r"(.*)(?:\.cue)", filepath)[0]+".txt"
# Parsing
with open(filepath,"r") as content, open(new_filepath,"w") as tracklist :
line = content.readline()
track_tab = {}
while line != "":
line = content.readline()
if "TRACK" in line:
title = re.findall(r"(?:TITLE\s\")(.*)(?:\")", content.readline())[0]
performer = re.findall(r"(?:PERFORMER\s\")(.*)(?:\")", content.readline())[0]
time = re.findall(r"(?:INDEX\s\d+\s)(.*)(?:\:\d+)", content.readline())[0]
track_string = time+" - "+performer+" - "+title+"\n" # makes a text line for any track
if show_state: # prints result if requested
print(track_string)
tracklist.write(track_string) # writes the line in the .txt file