This commit is contained in:
@@ -3,6 +3,7 @@ import glob
|
||||
import os
|
||||
import sys
|
||||
import subprocess
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
@@ -30,12 +31,17 @@ def get_song_length(file: Path) -> float:
|
||||
return -1
|
||||
|
||||
|
||||
def generate_pls(directory: str, output_file: str | None = None) -> None:
|
||||
def generate_playlist(
|
||||
directory: Path,
|
||||
pls_file: Path | None = None,
|
||||
json_file: Path | None = None,
|
||||
) -> None:
|
||||
"""Generate a .pls playlist file from all audio files in a directory.
|
||||
|
||||
Args:
|
||||
directory: Path to directory containing audio files
|
||||
output_file: Path to output .pls file (default: directory/playlist.pls)
|
||||
pls_file: Path to output .pls file (default: directory/playlist.pls)
|
||||
json_file: Path to output .json file (default: directory/playlist.json)
|
||||
"""
|
||||
if not os.path.isdir(directory):
|
||||
print(f"Error: Directory '{directory}' does not exist")
|
||||
@@ -58,10 +64,15 @@ def generate_pls(directory: str, output_file: str | None = None) -> None:
|
||||
audio_files.sort()
|
||||
|
||||
# Determine output file path
|
||||
if output_file is None:
|
||||
output_file = os.path.join(directory, "playlist.pls")
|
||||
if pls_file is None:
|
||||
pls_file = directory / "playlist.pls"
|
||||
|
||||
# Generate .pls content
|
||||
if json_file is None:
|
||||
json_file = directory / "playlist.json"
|
||||
|
||||
json_content = {}
|
||||
|
||||
# Generate output content
|
||||
pls_content = ["[playlist]"]
|
||||
pls_content.append(f"NumberOfEntries={len(audio_files)}")
|
||||
pls_content.append("")
|
||||
@@ -69,28 +80,48 @@ def generate_pls(directory: str, output_file: str | None = None) -> None:
|
||||
for idx, path in enumerate(audio_files, start=1):
|
||||
file = Path(path)
|
||||
|
||||
title = file.stem.replace('_', ' ')
|
||||
duration = get_song_length(file)
|
||||
|
||||
pls_content.append(f"File{idx}={file.absolute()}")
|
||||
pls_content.append(f"Title{idx}={file.stem.replace('_', ' ')}")
|
||||
pls_content.append(f"Length{idx}={get_song_length(file)}")
|
||||
pls_content.append(f"Title{idx}={title}")
|
||||
pls_content.append(f"Length{idx}={duration}")
|
||||
pls_content.append("")
|
||||
|
||||
json_content[str(file.absolute())] = {
|
||||
"index": idx,
|
||||
"file": str(file.absolute()),
|
||||
"title": title,
|
||||
"duration": duration,
|
||||
}
|
||||
|
||||
pls_content.append("Version=2")
|
||||
|
||||
# Write to file
|
||||
with open(output_file, "w") as f:
|
||||
with open(pls_file, "w") as f:
|
||||
f.write("\n".join(pls_content))
|
||||
|
||||
print(f"Generated '{output_file}' with {len(audio_files)} entries")
|
||||
with open(json_file, "w") as f:
|
||||
json.dump(json_content, f)
|
||||
|
||||
print(f"Generated '{pls_file}' with {len(audio_files)} entries")
|
||||
print(f"Generated '{json_file}' with {len(audio_files)} entries")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) < 2:
|
||||
print("Usage: je_te_met_en_pls.py <directory> [output_file.pls]")
|
||||
print(
|
||||
"Usage: je_te_met_en_pls.py <directory> [output_file.pls] [output_file.json]"
|
||||
)
|
||||
print("Example: je_te_met_en_pls.py /songs")
|
||||
print("Example: je_te_met_en_pls.py /songs /tmp/my_playlist.pls")
|
||||
print(
|
||||
"Example: je_te_met_en_pls.py /songs /tmp/my_playlist.pls /tmp/my_playlist.json"
|
||||
)
|
||||
sys.exit(1)
|
||||
|
||||
directory = sys.argv[1]
|
||||
output_file = sys.argv[2] if len(sys.argv) > 2 else None
|
||||
pls_file = Path(sys.argv[2]) if len(sys.argv) > 2 else None
|
||||
json_file = Path(sys.argv[3]) if len(sys.argv) > 3 else None
|
||||
|
||||
generate_pls(directory, output_file)
|
||||
generate_playlist(Path(directory), pls_file, json_file)
|
||||
|
||||
Reference in New Issue
Block a user