decorrupt_files/decorrupt_files.py
2025-09-15 19:42:18 +00:00

112 lines
2.8 KiB
Python
Executable File

import psycopg2
import subprocess
import os
import shlex
def get_conn():
conn = psycopg2.connect(
host="postgres",
database="mydatabase",
user="myuser",
password="mypassword",
port="5432" # Default PostgreSQL port
)
return conn
def process_file(file):
filename = file[0]
ref_x_status = file[1]
ref_12_status = file[2]
ref_16_status = file[3]
## process ref_x
if ref_x_status < 2:
process_ref(filename, "ref_x.MP4")
## process ref_12
if ref_12_status < 2:
process_ref(filename, "ref_12.MP4")
# ### process ref_16
# if ref_16_status < 2:
# process_ref(filename, "ref_16.MP4")
def update_table(filename, ref, status):
conn = get_conn()
try:
cursor = conn.cursor()
cursor.execute(f"UPDATE public.decorrupt SET {ref} = {status} WHERE filename = '{filename}'")
conn.commit()
except psycopg2.Error as e:
print(f"Error connecting to or querying the database: {e}")
finally:
# 6. Close Cursor and Connection
if cursor:
cursor.close()
if conn:
conn.close()
def process_ref(filename, ref):
print(f"processing file:{filename} ref:{ref}")
#set to processing
update_table(filename, ref.replace(".MP4", ""), 1)
# execute docker commnad
execute_docker(filename, ref)
# rename file
os.rename(f"/motherload/scan/{filename}_fixed-s1.MP4", f"/motherload/scan/{filename}_fixed-s1_{ref}")
#set to processed
update_table(filename, ref.replace(".MP4", ""), 2)
def execute_docker(filename, ref):
motherload_path = "/home/geezo/01_projects/decorrupt/motherload"
command = ["docker", "run", "--rm", "-v", f"{motherload_path}:/motherload", "untrunc", "-s", f"/motherload/reference/{ref}", f"/motherload/scan/{filename}"
]
try:
result = subprocess.run(command,
capture_output=True, text=True, check=True)
print(result.stdout)
except subprocess.CalledProcessError as e:
print(f"Error executing Docker command: {e}")
print(f"Stderr: {e.stderr}")
print(f"executing docker... {command}")
def get_files(conn):
try:
cursor = conn.cursor()
cursor.execute(f"SELECT * FROM public.decorrupt WHERE isresolved = 0 and isdeleted = 0 ORDER BY filename")
rows = cursor.fetchall()
except psycopg2.Error as e:
print(f"Error connecting to or querying the database: {e}")
finally:
# 6. Close Cursor and Connection
if cursor:
cursor.close()
if conn:
conn.close()
return rows
def main():
files = get_files(get_conn())
for file in files:
process_file(file)
if __name__ == "__main__":
main()