melon/core/melon.py

129 lines
3.8 KiB
Python
Raw Normal View History

2022-08-17 14:09:52 +07:00
import os
import mysql.connector as mariadb
import cv2
from pyffmpeg import FFmpeg
def CheckFileName(path):
filename, file_extension = os.path.splitext(path)
return filename
def CheckFileType(path):
filename, file_extension = os.path.splitext(path)
return file_extension
def CheckFileSize(path):
return os.path.getsize(path) # return in byte
def CheckFileHeight(path):
try:
vid = cv2.VideoCapture(path)
height = vid.get(cv2.CAP_PROP_FRAME_HEIGHT)
except Exception as e:
height = None
print('[DEBUG] Error: '+str(e))
return height
def CheckFileWidth(path):
try:
vid = cv2.VideoCapture(path)
width = vid.get(cv2.CAP_PROP_FRAME_WIDTH)
except Exception as e:
width = None
print('[DEBUG] Error: '+str(e))
return width
def CheckDir(storage):
return os.path.isdir(storage)
def CountDir(storage):
return len(os.listdir(storage))
def CountDb(dbin, table, portal_field, portal_value):
con = mariadb.connect(**dbin)
cursor = con.cursor()
cursor.execute(f"SELECT * FROM {table} WHERE {portal_field} = {portal_value} ;")
result = cursor.fetchall()
total = len(result)
con.close()
return total
def CountCompare(storage, dbin, table, portal_field, portal_value):
storage_count = CountDir(storage)
print('[DEBUG] Storage Count: '+str(storage_count))
record_count = CountDb(dbin, table, portal_field, portal_value)
print('[DEBUG] Record Count: '+str(record_count))
if storage_count == record_count:
print('[DEBUG] Sync well!')
return 'ok'
elif storage_count > record_count:
print('[DEBUG] New file detected!')
return 'new'
elif storage_count < record_count:
print('[DEBUG] Warning: Some videos is missing!')
return 'miss'
else:
print('[DEBUG] Unknown Error!')
return 'error'
def CreateVideoThumbnail(inf, outf):
FFmpeg().convert(inf, outf)
def CleanVideoThumbnail(storage, temp):
pass
def NewFileDetector(storage, dbin, table, temp):
# cek for list dari directori
new_file_count = 0
new_file = []
content = os.listdir(storage)
for row1 in content:
con = mariadb.connect(**dbin)
cursor = con.cursor()
cursor.execute("SELECT * FROM `"+table+"` WHERE id = '"+row1+"';")
result = cursor.fetchall()
detected = len(result)
con.close()
if detected == 0:
new_file_count += 1
name = CheckFileName(row1)
type = CheckFileType(row1)
size = CheckFileSize(storage+row1)
height = CheckFileHeight(storage+row1)
width = CheckFileWidth(storage+row1)
name_esc = name.replace(' ','\ ')
in_file = f'{storage}{name_esc}{type}'
tempthumb = f'{temp}{name_esc}{type}.jpg'
info = {
"file" : row1,
"name" : name,
"type" : type,
"size" : size,
"height" : height,
"width" : width,
"thumbnail": tempthumb
}
try:
CreateVideoThumbnail(in_file, tempthumb)
print(f'[DEBUG] FFmpeg is running')
print(f'[DEBUG] In File: {in_file}')
print(f'[DEBUG] Out File: {tempthumb}')
except Exception as e:
print(f'[DEBUG] ERROR: {e}')
new_file.append(info)
result = {
"count" : new_file_count,
"info" : new_file
}
return result
def MissingFileDetector(storage, dbin, table):
# cek for list dari database
pass
def NewFileInsertData():
pass
def MissingFileRemoveData():
pass