RasikhAli / Flask-MOV-to-WEBM-Converter

0 stars 0 forks source link

Enhancing Script Flexibility with JSON-based Parameters #1

Open Xer0bit opened 2 months ago

Xer0bit commented 2 months ago

An enhancement to your .mov to .webm conversion script: using a JSON file for parameter management. This will make the script more flexible, scalable, and maintainable.

Changes:

Create parameters.json:

{
    "low_quality": {
        "video_bitrate": "1M",
        "crf": "40",
        "cpu_used": "3",
        "audio_codec": "libopus",
        "audio_bitrate": "64k"
    },
    "medium_quality": {
        "video_bitrate": "2M",
        "crf": "30",
        "cpu_used": "2",
        "audio_codec": "libopus",
        "audio_bitrate": "128k"
    },
    "high_quality": {
        "video_bitrate": "4M",
        "crf": "20",
        "cpu_used": "1",
        "audio_codec": "libopus",
        "audio_bitrate": "256k"
    }
}

Update the Conversing script:

import os
import subprocess
import json

def load_parameters(quality):
    with open('parameters.json', 'r') as file:
        params = json.load(file)
    return params[quality]

def convert_mov_to_webm(input_folder, output_folder, quality):
    params = load_parameters(quality)

    for file_name in os.listdir(input_folder):
        if file_name.endswith('.mov'):
            input_file = os.path.join(input_folder, file_name)
            output_file = os.path.join(output_folder, os.path.splitext(file_name)[0] + '.webm')
            command = [
                ffmpeg_path,
                '-i', input_file,
                '-c:v', 'libvpx-vp9',
                '-b:v', params['video_bitrate'],
                '-crf', params['crf'],
                '-cpu-used', params['cpu_used'],
                '-c:a', params['audio_codec'],
                '-b:a', params['audio_bitrate'],
                output_file
            ]
            subprocess.run(command)
# convert_mov_to_webm('input_folder', 'output_folder', 'medium_quality')

This will allow us to easily adjust settings for different quality levels without modifying the script.

Thanks

RasikhAli commented 2 months ago

That is a great addition, thanks for that.