-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode_playlist.py
More file actions
94 lines (76 loc) · 3.4 KB
/
Copy pathencode_playlist.py
File metadata and controls
94 lines (76 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import urllib.parse
import argparse
import os
import re
PROXY_PREFIX = "http://127.0.0.1:8888/proxy/m3u?url="
def encode_and_prefix_url(url_to_process):
trimmed_url = url_to_process.strip()
if not trimmed_url:
return ""
encoded_url = urllib.parse.quote(trimmed_url, safe='')
return f"{PROXY_PREFIX}{encoded_url}"
def process_m3u8_file(input_filepath, output_filepath):
lines_processed = 0
urls_modified = 0
try:
with open(input_filepath, 'r', encoding='utf-8') as infile, \
open(output_filepath, 'w', encoding='utf-8') as outfile:
for line in infile:
lines_processed += 1
stripped_line = line.strip()
if not stripped_line:
outfile.write(line)
continue
modified_line_content = None
if stripped_line.startswith(("#EXT-X-KEY:", "#EXT-X-MEDIA:")):
match = re.match(r'(.*URI=")([^"]+)(".*)', stripped_line)
if match:
pre_uri_part = match.group(1)
original_uri = match.group(2)
post_uri_part = match.group(3)
if original_uri.strip():
new_uri = encode_and_prefix_url(original_uri)
modified_line_content = f"{pre_uri_part}{new_uri}{post_uri_part}\n"
urls_modified += 1
else:
modified_line_content = line
else:
modified_line_content = line
elif stripped_line.startswith("#"):
modified_line_content = line
else:
new_url = encode_and_prefix_url(stripped_line)
if new_url:
modified_line_content = new_url + '\n'
urls_modified +=1
else:
modified_line_content = '\n'
if modified_line_content is not None:
outfile.write(modified_line_content)
else:
outfile.write(line)
print(f"Successfully processed '{input_filepath}' ({lines_processed} lines read).")
print(f"{urls_modified} URLs/URIs were modified.")
print(f"Output saved to '{output_filepath}'")
except FileNotFoundError:
print(f"Error: Input file '{input_filepath}' not found.")
except Exception as e:
print(f"An error occurred during processing: {e}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Processes an M3U8 file to URL-encode and prefix URLs/URIs within it.",
formatter_class=argparse.RawTextHelpFormatter
)
parser.add_argument(
"input_file",
help="The path to the input M3U8 file (e.g., playlist.m3u8)."
)
args = parser.parse_args()
input_m3u8_file = args.input_file
path_without_ext, original_ext = os.path.splitext(input_m3u8_file)
output_m3u8_file = f"{path_without_ext}_encoded{original_ext}"
if input_m3u8_file == output_m3u8_file:
print(f"Error: Input and output filenames are the same ('{input_m3u8_file}').")
print("Please rename your input file or choose a different naming scheme if it has '_encoded' already.")
else:
process_m3u8_file(input_m3u8_file, output_m3u8_file)