melhorado e compartibilidade de x265 decoder cpu e encoder x264
This commit is contained in:
@@ -15,6 +15,7 @@ class FFmpegEngine:
|
||||
state.log("⚠️ AVISO: Nenhum perfil FFmpeg ativo! Usando defaults.")
|
||||
|
||||
def get_file_info(self, filepath):
|
||||
"""Extrai metadados do arquivo usando ffprobe"""
|
||||
cmd = ['ffprobe', '-v', 'quiet', '-print_format', 'json', '-show_streams', '-show_format', filepath]
|
||||
try:
|
||||
output = subprocess.check_output(cmd).decode('utf-8')
|
||||
@@ -33,56 +34,68 @@ class FFmpegEngine:
|
||||
metadata = self.get_file_info(input_file)
|
||||
if not metadata: raise Exception("Arquivo inválido.")
|
||||
|
||||
cmd = ['ffmpeg', '-y']
|
||||
|
||||
# --- CONFIGURAÇÃO DE HARDWARE OTIMIZADA ---
|
||||
video_filters = []
|
||||
# Descobre o codec de vídeo de entrada
|
||||
video_stream = next((s for s in metadata['streams'] if s['codec_type'] == 'video'), None)
|
||||
input_codec = video_stream['codec_name'] if video_stream else 'unknown'
|
||||
|
||||
# --- DETECÇÃO DE HARDWARE (Haswell Safe Logic) ---
|
||||
# Intel 4ª Geração (Haswell):
|
||||
# Decode: Suporta H264, MPEG2. NÃO SUPORTA HEVC/x265.
|
||||
|
||||
can_hw_decode = False
|
||||
hw_decodable_codecs = ['h264', 'mpeg2video', 'vc1', 'mjpeg']
|
||||
|
||||
if 'vaapi' in p.video_codec:
|
||||
# TENTATIVA OTIMIZADA: HWAccel habilitado na entrada
|
||||
# Isso reduz a CPU drasticamente se o decode for suportado
|
||||
cmd.extend(['-hwaccel', 'vaapi'])
|
||||
cmd.extend(['-hwaccel_device', '/dev/dri/renderD128'])
|
||||
cmd.extend(['-hwaccel_output_format', 'vaapi'])
|
||||
|
||||
# Não precisamos de 'hwupload' se o output format já é vaapi
|
||||
# Mas vamos deixar o filtro scale_vaapi caso precise redimensionar (opcional)
|
||||
# video_filters.append('scale_vaapi=format=nv12')
|
||||
if input_codec in hw_decodable_codecs:
|
||||
can_hw_decode = True
|
||||
else:
|
||||
state.log(f"⚙️ Modo Híbrido (Haswell): CPU lendo {input_codec} -> GPU codificando.")
|
||||
|
||||
cmd = ['ffmpeg', '-y']
|
||||
|
||||
# --- INPUT (LEITURA) ---
|
||||
if 'vaapi' in p.video_codec:
|
||||
# Inicializa o dispositivo VAAPI
|
||||
cmd.extend(['-init_hw_device', 'vaapi=intel:/dev/dri/renderD128'])
|
||||
cmd.extend(['-filter_hw_device', 'intel'])
|
||||
|
||||
if can_hw_decode:
|
||||
# Se a GPU sabe ler, usa aceleração total
|
||||
cmd.extend(['-hwaccel', 'vaapi'])
|
||||
cmd.extend(['-hwaccel_output_format', 'vaapi'])
|
||||
cmd.extend(['-hwaccel_device', 'intel'])
|
||||
|
||||
elif 'qsv' in p.video_codec:
|
||||
cmd.extend(['-hwaccel', 'qsv', '-c:v', 'h264_qsv'])
|
||||
|
||||
cmd.extend(['-hwaccel', 'qsv', '-c:v', 'h264_qsv'])
|
||||
elif 'nvenc' in p.video_codec:
|
||||
cmd.extend(['-hwaccel', 'cuda'])
|
||||
|
||||
# Input e Threads (Limita CPU se cair pra software decode)
|
||||
cmd.extend(['-threads', '4', '-i', input_file])
|
||||
|
||||
# --- VÍDEO ---
|
||||
# --- PROCESSAMENTO E SAÍDA ---
|
||||
cmd.extend(['-map', '0:v:0'])
|
||||
video_filters = []
|
||||
|
||||
if p.video_codec == 'copy':
|
||||
cmd.extend(['-c:v', 'copy'])
|
||||
else:
|
||||
cmd.extend(['-c:v', p.video_codec])
|
||||
|
||||
# Filtros
|
||||
# Se for VAAPI Híbrido (leitura CPU), precisamos subir pra GPU
|
||||
if 'vaapi' in p.video_codec and not can_hw_decode:
|
||||
video_filters.append('format=nv12,hwupload')
|
||||
|
||||
if video_filters:
|
||||
cmd.extend(['-vf', ",".join(video_filters)])
|
||||
|
||||
# Qualidade
|
||||
if 'vaapi' in p.video_codec:
|
||||
# -qp é o modo Qualidade Constante do VAAPI
|
||||
# Se der erro, remova -qp e use -b:v 5M
|
||||
cmd.extend(['-qp', str(p.crf)])
|
||||
|
||||
cmd.extend(['-c:v', p.video_codec])
|
||||
|
||||
if 'vaapi' in p.video_codec:
|
||||
cmd.extend(['-qp', str(p.crf)])
|
||||
elif 'nvenc' in p.video_codec:
|
||||
cmd.extend(['-cq', str(p.crf), '-preset', p.preset])
|
||||
|
||||
elif 'libx264' in p.video_codec:
|
||||
cmd.extend(['-crf', str(p.crf), '-preset', p.preset])
|
||||
|
||||
# --- ÁUDIO (AAC Stereo) ---
|
||||
# --- ÁUDIO (AAC) ---
|
||||
allowed_langs = [l.strip().lower() for l in (p.audio_langs or "").split(',')]
|
||||
audio_streams = [s for s in metadata['streams'] if s['codec_type'] == 'audio']
|
||||
|
||||
@@ -91,14 +104,13 @@ class FFmpegEngine:
|
||||
lang = s.get('tags', {}).get('language', 'und').lower()
|
||||
if not allowed_langs or lang in allowed_langs or 'und' in allowed_langs:
|
||||
cmd.extend(['-map', f'0:{s["index"]}'])
|
||||
# AAC é leve e compatível
|
||||
cmd.extend([f'-c:a:{acount}', 'aac', f'-b:a:{acount}', '192k', f'-ac:{acount}', '2'])
|
||||
acount += 1
|
||||
|
||||
if acount == 0 and audio_streams:
|
||||
cmd.extend(['-map', '0:a:0', '-c:a', 'aac', '-b:a', '192k'])
|
||||
|
||||
# --- LEGENDAS (Copy) ---
|
||||
# --- LEGENDAS ---
|
||||
sub_allowed = [l.strip().lower() for l in (p.subtitle_langs or "").split(',')]
|
||||
sub_streams = [s for s in metadata['streams'] if s['codec_type'] == 'subtitle']
|
||||
|
||||
@@ -110,10 +122,8 @@ class FFmpegEngine:
|
||||
cmd.extend([f'-c:s:{scount}', 'copy'])
|
||||
scount += 1
|
||||
|
||||
# Metadados
|
||||
clean_title = os.path.splitext(os.path.basename(output_file))[0]
|
||||
cmd.extend(['-metadata', f'title={clean_title}'])
|
||||
cmd.append(output_file)
|
||||
|
||||
state.log(f"🛠️ CMD: {' '.join(cmd)}")
|
||||
return cmd
|
||||
Reference in New Issue
Block a user