-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.py
More file actions
121 lines (95 loc) · 3.42 KB
/
Main.py
File metadata and controls
121 lines (95 loc) · 3.42 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import os
import sys
import subprocess
import ftplib
import configparser
from dotenv import load_dotenv
def get_subtitle_streams(input_file):
# Run ffmpeg to get the subtitle streams
command = [
"ffmpeg",
"-i",
input_file
]
result = subprocess.run(command, capture_output=True, text=True)
# Search for subtitle streams in the ffmpeg output
subtitle_streams = []
for line in result.stderr.splitlines():
if "Stream" in line and "Subtitle" in line:
# Extract the stream ID and language
parts = line.split(":")
if len(parts) >= 3:
stream_info = parts[1].split("(")[-1].split(")")[0]
stream_id = parts[1].split("(")[0].replace("Stream #0", "")
subtitle_streams.append(f"0:s:{stream_id}")
return subtitle_streams
def upload_to_ftp(local_file):
load_dotenv("config.env")
server = os.getenv("FTP_SERVER")
username = os.getenv("FTP_USERNAME")
password = os.getenv("FTP_PASSWORD")
remote_path = os.getenv("FTP_REMOTE_PATH")
try:
with ftplib.FTP(server) as ftp:
ftp.login(username, password)
ftp.cwd(remote_path)
with open(local_file, "rb") as file:
ftp.storbinary(f"STOR {os.path.basename(local_file)}", file)
print(f"Uploaded {local_file} to {server}/{remote_path}")
except Exception as e:
print(f"FTP upload failed: {e}")
def convert_to_mp4(input_file):
base, _ = os.path.splitext(input_file)
output_file = base + ".mp4"
print(f"\nConverting:\n {input_file}\n -> {output_file}\n")
# Get subtitle streams
subtitle_streams = get_subtitle_streams(input_file)
# If no subtitles are found, show a message
if not subtitle_streams:
print("No subtitles found.")
# Basic ffmpeg command with dynamic subtitle selection
command = [
"ffmpeg",
"-hide_banner",
"-loglevel", "warning",
"-i", input_file,
"-c:v", "hevc_nvenc",
"-c:a", "aac",
"-c:s", "mov_text",
"-map", "0:v:0?",
"-map", "0:a:0?",
"-avoid_negative_ts", "make_zero"
]
# Add the subtitle streams found to the command
for subtitle in subtitle_streams:
command.append("-map")
command.append(f"{subtitle}?")
command.append("-y")
command.append(output_file)
try:
subprocess.run(command, check=True)
print("Conversion successful!")
# Upload to my server using FTP
upload_to_ftp(output_file)
# Optionally delete the original file after successful conversion
# os.remove(input_file)
# print(f"Deleted file: {input_file}")
except subprocess.CalledProcessError as e:
print(f"Error converting {input_file}:\n{e}")
def main():
if len(sys.argv) < 2:
folder = input("Enter the folder path to process: ")
else:
folder = sys.argv[1]
if not os.path.isdir(folder):
print(f"The folder '{folder}' does not exist or is not valid.")
sys.exit(1)
print(f"Searching for .avi and .mkv files in: {folder}")
for root, dirs, files in os.walk(folder):
for file in files:
file_lower = file.lower()
if file_lower.endswith(".avi") or file_lower.endswith(".mkv"):
input_path = os.path.join(root, file)
convert_to_mp4(input_path)
if __name__ == "__main__":
main()