import os
import subprocess
from concurrent.futures import ThreadPoolExecutor

def convert_webm_to_mp4(input_file, output_file):
    try:
        subprocess.run(['ffmpeg', '-i', input_file, output_file])
        print(f'Converted {input_file} to {output_file}')
        # Remove the original .webm file
        os.remove(input_file)
    except Exception as e:
        print(f'Error converting {input_file}: {e}')

# def main(root_dir):
#     for root, _, files in os.walk(root_dir):
#         for file in files:
#             if file.lower().endswith('.webm'):
#                 input_file = os.path.join(root, file)
#                 output_file = os.path.join(root, file.lower().replace('.webm', '.mp4'))
#                 convert_webm_to_mp4(input_file, output_file)

#                 # Remove the original .webm file
#                 os.remove(input_file)

def main(root_dir):
    with ThreadPoolExecutor() as executor:
        for root, _, files in os.walk(root_dir):
            for file in files:
                if file.lower().endswith('.webm'):
                    input_file = os.path.join(root, file)
                    output_file = os.path.join(root, file.lower().replace('.webm', '.mp4'))
                    executor.submit(convert_webm_to_mp4, input_file, output_file)
                    # You can control the number of concurrent conversions by adjusting the ThreadPoolExecutor's max_workers parameter.


if __name__ == "__main__":
    directory = "/var/www/html/2M_Shows/Tarik_El_Ward"  # Specify the directory containing the .webm files
    main(directory)