-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
64 lines (53 loc) · 2.13 KB
/
main.py
File metadata and controls
64 lines (53 loc) · 2.13 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
import os
import logging
from config import MEDIA_ROOT, DB_PATH
from db_handler import DatabaseHandler
from scanner import MediaScanner
from converter import MediaConverter
def setup_logging():
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('media_converter.log'),
logging.StreamHandler()
]
)
def main():
setup_logging()
logging.info("Starting media conversion process")
db = DatabaseHandler(DB_PATH)
scanner = MediaScanner(MEDIA_ROOT)
converter = MediaConverter()
for file_path in scanner.scan_directory():
try:
# Check if file has already been processed
status, stored_codec = db.get_file_status(file_path)
if status == "completed":
logging.info(f"Skipping {file_path} already {stored_codec}")
continue
# Check current codec
current_codec = converter.get_video_codec(file_path)
# Skip if already H.265
if current_codec == "hevc":
db.update_file_status(file_path, "completed", "hevc")
continue
# Prepare output path
dirname = os.path.dirname(file_path)
filename = os.path.basename(file_path)
output_path = os.path.join(dirname, f"converted_{filename}")
# Convert file
logging.info(f"Converting {file_path}")
if converter.convert_to_h265(file_path, output_path):
# Replace original file with converted file
os.replace(output_path, file_path)
db.update_file_status(file_path, "completed", "hevc")
logging.info(f"Successfully converted {file_path}")
else:
db.update_file_status(file_path, "failed")
logging.error(f"Failed to convert {file_path}")
except Exception as e:
logging.error(f"Error processing {file_path}: {e}")
db.update_file_status(file_path, "failed")
if __name__ == "__main__":
main()