diff --git a/m3u-cue-converter.sh b/m3u-cue-converter.sh new file mode 100755 index 0000000..faf15a3 --- /dev/null +++ b/m3u-cue-converter.sh @@ -0,0 +1,160 @@ +#!/bin/bash + +# Simple M3U to CUE Converter Script +# Converts M3U playlist files to basic CUE format + +# Function to display usage +show_usage() { + echo "Usage: $0 [OPTIONS] [output.cue]" + echo "" + echo "Options:" + echo " -t, --title TITLE Set playlist title (default: derived from filename)" + echo " -r, --relative Use relative paths (strip directory paths)" + echo " -h, --help Show this help message" + echo "" + echo "Examples:" + echo " $0 Mix.m3u" + echo " $0 Mix.m3u Mix.cue" + echo " $0 --title \"My Playlist\" --relative Mix.m3u" +} + +# Default options +PLAYLIST_TITLE="" +USE_RELATIVE_PATHS=false +INPUT_FILE="" +OUTPUT_FILE="" + +# Parse command line arguments +while [[ $# -gt 0 ]]; do + case $1 in + -t|--title) + PLAYLIST_TITLE="$2" + shift 2 + ;; + -r|--relative) + USE_RELATIVE_PATHS=true + shift + ;; + -h|--help) + show_usage + exit 0 + ;; + -*) + echo "Unknown option: $1" + show_usage + exit 1 + ;; + *) + if [ -z "$INPUT_FILE" ]; then + INPUT_FILE="$1" + elif [ -z "$OUTPUT_FILE" ]; then + OUTPUT_FILE="$1" + else + echo "Too many arguments" + show_usage + exit 1 + fi + shift + ;; + esac +done + +# Check if input file is provided +if [ -z "$INPUT_FILE" ]; then + echo "Error: Input M3U file is required" + show_usage + exit 1 +fi + +# Check if input file exists +if [ ! -f "$INPUT_FILE" ]; then + echo "Error: Input file '$INPUT_FILE' not found" + exit 1 +fi + +# Set default output file if not provided +if [ -z "$OUTPUT_FILE" ]; then + OUTPUT_FILE="${INPUT_FILE%.*}.cue" +fi + +# Set default playlist title if not provided +if [ -z "$PLAYLIST_TITLE" ]; then + PLAYLIST_TITLE=$(basename "$INPUT_FILE" .m3u) +fi + +# Function to extract filename from path +get_filename() { + local filepath="$1" + if [ "$USE_RELATIVE_PATHS" = true ]; then + basename "$filepath" + else + echo "$filepath" + fi +} + +# Function to determine file format from extension +get_file_format() { + local filepath="$1" + local ext="${filepath##*.}" + ext=$(echo "$ext" | tr '[:upper:]' '[:lower:]') + + case "$ext" in + "flac") echo "FLAC" ;; + "wav") echo "WAVE" ;; + "mp3") echo "MP3" ;; + "ogg") echo "OGG" ;; + *) echo "BINARY" ;; + esac +} + +echo "Converting M3U to CUE format..." +echo "Input: $INPUT_FILE" +echo "Output: $OUTPUT_FILE" +echo "" + +# Start writing CUE file +{ + echo "TITLE \"$PLAYLIST_TITLE\"" +} > "$OUTPUT_FILE" + +# Process M3U file +track_num=1 +processed_files=0 + +while IFS= read -r line || [ -n "$line" ]; do + # Skip empty lines and all comment lines + if [ -z "$line" ] || [[ "$line" == "#"* ]]; then + continue + fi + + # Process file path + filepath="$line" + filename=$(get_filename "$filepath") + file_format=$(get_file_format "$filepath") + + # Add track to CUE file + { + echo "FILE \"$filename\" $file_format" + printf " TRACK %02d AUDIO\n" $track_num + echo " INDEX 01 00:00:00" + } >> "$OUTPUT_FILE" + + echo " Track $track_num: $filename" + + ((track_num++)) + ((processed_files++)) +done < "$INPUT_FILE" + +echo "" +echo "Conversion completed successfully!" +echo "Processed $processed_files tracks" +echo "CUE file saved as: $OUTPUT_FILE" + +# Validate output +if [ -f "$OUTPUT_FILE" ] && [ -s "$OUTPUT_FILE" ]; then + echo "Output file validation: OK" + exit 0 +else + echo "Error: Output file was not created properly" + exit 1 +fi