| import cv2
|
| import os
|
| import shutil
|
| import subprocess
|
|
|
| def extraer_ultimo_fotograma(video_path, output_image_path):
|
| """
|
| Extrae el último fotograma de un video y lo guarda como una imagen JPG con calidad 100%.
|
| Luego copia el video a la carpeta '/content/video_output' con numeración secuencial.
|
|
|
| :param video_path: Ruta del archivo de video (ej. '/content/video.mp4')
|
| :param output_image_path: Ruta donde se guardará el fotograma como imagen (ej. '/content/img_fragmento.jpg')
|
| """
|
|
|
| try:
|
|
|
| video = cv2.VideoCapture(video_path)
|
|
|
|
|
| total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
|
|
|
|
|
| video.set(cv2.CAP_PROP_POS_FRAMES, total_frames - 1)
|
|
|
|
|
| ret, frame = video.read()
|
|
|
|
|
| if ret:
|
|
|
| cv2.imwrite(output_image_path, frame, [int(cv2.IMWRITE_JPEG_QUALITY), 100])
|
| print(f'Último fotograma guardado con calidad 100% (OpenCV)')
|
| else:
|
| print('Error al leer el último fotograma (OpenCV)')
|
|
|
|
|
| video.release()
|
|
|
| except Exception as e:
|
| print(f"Error al usar OpenCV: {e}")
|
|
|
|
|
|
|
| duracion_cmd = [
|
| "ffprobe",
|
| "-v", "error",
|
| "-show_entries", "format=duration",
|
| "-of", "default=noprint_wrappers=1:nokey=1",
|
| video_path
|
| ]
|
| duracion = float(subprocess.check_output(duracion_cmd))
|
|
|
|
|
| tiempo_busqueda = str(duracion - 0.1)
|
|
|
|
|
| comando = [
|
| "ffmpeg",
|
| "-ss", tiempo_busqueda,
|
| "-i", video_path,
|
| "-vframes", "1",
|
| "-qscale:v", "2",
|
| output_image_path
|
| ]
|
|
|
|
|
| subprocess.run(comando)
|
| print(f'Último fotograma guardado con calidad 100% (FFmpeg)')
|
|
|
|
|
| output_folder = '/content/Vidu_Studio/video_output'
|
| if not os.path.exists(output_folder):
|
| os.makedirs(output_folder)
|
| print(f'Carpeta creada: {output_folder}')
|
|
|
|
|
| existing_videos = sorted([f for f in os.listdir(output_folder) if f.endswith('.mp4')])
|
|
|
| if existing_videos:
|
|
|
| last_video = existing_videos[-1]
|
| last_number = int(last_video.split('.')[0])
|
| new_number = last_number + 1
|
| else:
|
|
|
| new_number = 1
|
|
|
|
|
| new_video_name = f'{new_number:05}.mp4'
|
| new_video_path = os.path.join(output_folder, new_video_name)
|
|
|
|
|
| shutil.copy(video_path, new_video_path)
|
| print(f'Video copiado como {new_video_path}')
|
|
|
|
|
|
|
| video_path = '/content/Vidu_Studio/video.mp4'
|
| output_image_path = '/tmp/img_fragmento.jpg'
|
|
|
| extraer_ultimo_fotograma(video_path, output_image_path) |