# -*- coding: utf-8 -*-

import subprocess
import os

file_name = "screenshot.png"
audio_file = "audio.mp3"

try:
    if not os.path.exists(file_name):
        raise FileNotFoundError(file_name)

    if not os.path.exists(audio_file):
        raise FileNotFoundError(audio_file)

    command = [
        "ffmpeg", "-y",
        "-loop", "1",
        "-framerate", "1",
        "-i", file_name,
        "-stream_loop", "-1",
        "-i", audio_file,
        "-t", "120",

        "-vf", "scale=640:360,format=yuv420p",
        "-c:v", "libx264",
        "-preset", "ultrafast",
        "-tune", "stillimage",
        "-crf", "35",
        "-r", "1",
        "-g", "10",
        "-threads", "1",

        "-c:a", "aac",
        "-b:a", "64k",
        "-ac", "1",
        "-ar", "22050",

        "-f", "hls",
        "-hls_time", "10",
        "-hls_list_size", "0",
        "-hls_segment_filename", "segment_%03d.ts",
        "locandina.m3u8"
    ]

    subprocess.run(command, check=True)

    print("File M3U8 creato: locandina.m3u8")

except Exception as e:
    print(f"Errore: {e}")