/ scripts / fix-m3u-paths.py
fix-m3u-paths.py
 1  #!/usr/bin/env python3
 2  """
 3  Fix M3U file paths for VPS or Docker deployment
 4  Usage: python3 fix-m3u-paths.py input.m3u output.m3u [--docker|--vps]
 5  """
 6  
 7  import sys
 8  from pathlib import Path
 9  
10  def fix_m3u_paths(input_file, output_file, mode='vps'):
11      """Convert relative paths to absolute paths for VPS or Docker"""
12      
13      if mode == 'docker':
14          base_path = '/app/music'
15      else:  # vps
16          base_path = '/home/glenneth/Music'
17      
18      with open(input_file, 'r', encoding='utf-8') as f_in:
19          with open(output_file, 'w', encoding='utf-8') as f_out:
20              for line in f_in:
21                  line = line.rstrip('\n')
22                  
23                  # Keep #EXTM3U and #EXTINF lines as-is
24                  if line.startswith('#'):
25                      f_out.write(line + '\n')
26                  # Convert file paths
27                  elif line.strip():
28                      # Remove leading/trailing whitespace
29                      path = line.strip()
30                      # If it's already an absolute path, keep it
31                      if path.startswith('/'):
32                          f_out.write(path + '\n')
33                      else:
34                          # Make it absolute
35                          full_path = f"{base_path}/{path}"
36                          f_out.write(full_path + '\n')
37                  else:
38                      f_out.write('\n')
39      
40      print(f"Converted {input_file} -> {output_file}")
41      print(f"Mode: {mode}")
42      print(f"Base path: {base_path}")
43  
44  def main():
45      if len(sys.argv) < 3:
46          print("Usage: python3 fix-m3u-paths.py input.m3u output.m3u [--docker|--vps]")
47          print("  --docker: Use /app/music/ prefix (for Docker container)")
48          print("  --vps:    Use /home/glenneth/Music/ prefix (default)")
49          sys.exit(1)
50      
51      input_file = sys.argv[1]
52      output_file = sys.argv[2]
53      mode = 'vps'
54      
55      if len(sys.argv) > 3:
56          if sys.argv[3] == '--docker':
57              mode = 'docker'
58          elif sys.argv[3] == '--vps':
59              mode = 'vps'
60      
61      if not Path(input_file).exists():
62          print(f"Error: Input file '{input_file}' not found")
63          sys.exit(1)
64      
65      fix_m3u_paths(input_file, output_file, mode)
66  
67  if __name__ == "__main__":
68      main()