python.video_converter_v3/app/class_file_convert.py

110 lines
No EOL
3.7 KiB
Python

import logging
import asyncio
import time
from app.class_file_convert_read_out import Process
class Convert:
def __init__(self, websocket, cfg, obj_path):
self.yaml = cfg
self.obj_path = obj_path
self.obj_websocket = websocket
self.obj_process = Process(websocket)
self.semaphore = asyncio.Semaphore(self.yaml["task_max"])
self.active_tasks = set()
self.active_process = set()
logging.info("Video Convert Start here")
async def snake_waiting(self):
async with self.semaphore:
for obj_id, obj in list(self.obj_path.paths.items()):
if obj.status is None:
obj.task = asyncio.create_task(self.convert_video(obj))
self.active_tasks.add(obj)
logging.info(f"Warteschlange started Auftrag - {obj.task}")
print(self.obj_path.paths)
async def convert_video(self, obj):
"""Startet die Videokonvertierung asynchron."""
obj.convert_start = time.time()
command = self.convert_cmd(obj)
logging.info(f"Starte Konvertierung: {command}")
try:
# Starte den Subprozess asynchron
obj.process = await asyncio.create_subprocess_exec(
*command,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
self.active_process.add(obj)
obj.process_start = time.time()
await self.obj_process.read_out(obj)
await obj.process.wait()
# Prozess beendet, Status auswerten
if obj.process.returncode == 0:
obj.status = 0
result = "Finished"
else:
obj.status = 1
result = "Failure"
# Statistik
#obj_stat = Sc()
#obj_stat.save_stat(obj.get_vars())
except Exception as e:
obj.status = 2
logging.error(f"Fehler in video_convert(): {e}")
finally:
logging.info(f"Prozess {result}({obj.process.returncode}): {obj.source_file_name}")
await self.obj_websocket.send_websocket(obj.to_dict())
self.active_process.discard(obj)
self.active_tasks.discard(obj)
obj.convert_end = time.time()
def convert_cmd(self, obj):
command_convert = [
"ffmpeg", "-y", "-i", obj.source_file,
"-map", "0:0",
"-c:v", "libsvtav1",
"-preset", "5",
"-crf", "30",
"-g", "240",
"-pix_fmt", "yuv420p10le",
"-svtav1-params", "tune=0:film-grain=8",
]
if len(obj.streams_audio):
for audio_stream in obj.streams_audio:
if audio_stream.get("tags", {}).get("language", None) in self.yaml["audio"]["language"]:
command_convert.extend([
"-map", f"0:{audio_stream['index']}",
f"-c:a", "libopus",
f"-b:a", "320k",
f"-ac", str(audio_stream['channels'])
])
# Subtitle-Streams einbinden
if len(obj.streams_subtitle):
for subtitle_stream in obj.streams_subtitle:
if subtitle_stream.get("codec_name") not in self.yaml["subtitle"]["blacklist"]:
if subtitle_stream.get("tags", {}).get("language", None) in self.yaml["subtitle"]["language"]:
command_convert.extend([
"-map", f"0:{subtitle_stream['index']}",
])
command_convert.append(obj.target_file)
return command_convert
def snake_update(self):
print(self.obj_path.paths)