97 lines
2.6 KiB
Python
Executable File
97 lines
2.6 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
import glob
|
|
import os
|
|
import sys
|
|
import subprocess
|
|
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_pls(directory: str, output_file: str | 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)
|
|
"""
|
|
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 output_file is None:
|
|
output_file = os.path.join(directory, "playlist.pls")
|
|
|
|
# Generate .pls 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)
|
|
|
|
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("")
|
|
|
|
pls_content.append("Version=2")
|
|
|
|
# Write to file
|
|
with open(output_file, "w") as f:
|
|
f.write("\n".join(pls_content))
|
|
|
|
print(f"Generated '{output_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("Example: je_te_met_en_pls.py /songs")
|
|
print("Example: je_te_met_en_pls.py /songs /tmp/my_playlist.pls")
|
|
sys.exit(1)
|
|
|
|
directory = sys.argv[1]
|
|
output_file = sys.argv[2] if len(sys.argv) > 2 else None
|
|
|
|
generate_pls(directory, output_file)
|