128 lines
3.4 KiB
Python
Executable File
128 lines
3.4 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
import glob
|
|
import os
|
|
import sys
|
|
import subprocess
|
|
import json
|
|
from pathlib import Path
|
|
|
|
|
|
def get_song_length(file: Path) -> float:
|
|
ret = subprocess.run(
|
|
[
|
|
"ffprobe",
|
|
"-i",
|
|
str(file.absolute()),
|
|
"-show_entries",
|
|
"format=duration",
|
|
"-v",
|
|
"quiet",
|
|
"-of",
|
|
"csv=p=0",
|
|
],
|
|
stdout=subprocess.PIPE,
|
|
)
|
|
if ret.returncode != 0:
|
|
return -1
|
|
|
|
try:
|
|
return float(ret.stdout)
|
|
except ValueError:
|
|
return -1
|
|
|
|
|
|
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
|
|
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")
|
|
sys.exit(1)
|
|
|
|
# Audio file extensions to search for
|
|
audio_extensions = ["*.mp3", "*.ogg", "*.flac", "*.wav", "*.m4a", "*.aac", "*.opus"]
|
|
|
|
# Collect all audio files
|
|
audio_files = []
|
|
for ext in audio_extensions:
|
|
pattern = os.path.join(directory, ext)
|
|
audio_files.extend(glob.glob(pattern))
|
|
|
|
if not audio_files:
|
|
print(f"Warning: No audio files found in '{directory}'")
|
|
return
|
|
|
|
# Sort files alphabetically
|
|
audio_files.sort()
|
|
|
|
# Determine output file path
|
|
if pls_file is None:
|
|
pls_file = directory / "playlist.pls"
|
|
|
|
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("")
|
|
|
|
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}={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(pls_file, "w") as f:
|
|
f.write("\n".join(pls_content))
|
|
|
|
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] [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]
|
|
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_playlist(Path(directory), pls_file, json_file)
|