Skip to content

Instantly share code, notes, and snippets.

@cassidoo
Created January 25, 2025 00:41
Show Gist options
  • Select an option

  • Save cassidoo/2e4e71c593c3ebb527c86321547e7d08 to your computer and use it in GitHub Desktop.

Select an option

Save cassidoo/2e4e71c593c3ebb527c86321547e7d08 to your computer and use it in GitHub Desktop.

Revisions

  1. cassidoo revised this gist Jan 25, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion generateLut.js
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,6 @@ function generateLutFilter(hexColorsToExclude) {
    return { r, g, b };
    }

    // Create the LUT filter string
    let lutFilter = "lutrgb=";
    let rConditions = [];
    let gConditions = [];
    @@ -33,4 +32,5 @@ const hexColorsToExclude = ["008000", "007ACC"];
    const lutFilter = generateLutFilter(hexColorsToExclude);

    const ffmpegCommand = `ffmpeg -i input.mp4 -vf "${lutFilter}" -c:a copy output.mp4`;

    console.log(ffmpegCommand);
  2. cassidoo created this gist Jan 25, 2025.
    36 changes: 36 additions & 0 deletions generateLut.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    function generateLutFilter(hexColorsToExclude) {
    function hexToRgb(hex) {
    const bigint = parseInt(hex, 16);
    const r = (bigint >> 16) & 255;
    const g = (bigint >> 8) & 255;
    const b = bigint & 255;
    return { r, g, b };
    }

    // Create the LUT filter string
    let lutFilter = "lutrgb=";
    let rConditions = [];
    let gConditions = [];
    let bConditions = [];

    for (const hexColor of hexColorsToExclude) {
    const { r, g, b } = hexToRgb(hexColor);
    rConditions.push(`if(eq(val\\,${r})\\,${r}\\,`);
    gConditions.push(`if(eq(val\\,${g})\\,${g}\\,`);
    bConditions.push(`if(eq(val\\,${b})\\,${b}\\,`);
    }

    const rFilter = rConditions.length > 0 ? rConditions.join("") + "255-val" + ")".repeat(rConditions.length) : "255-val";
    const gFilter = gConditions.length > 0 ? gConditions.join("") + "255-val" + ")".repeat(gConditions.length) : "255-val";
    const bFilter = bConditions.length > 0 ? bConditions.join("") + "255-val" + ")".repeat(bConditions.length) : "255-val";

    lutFilter += `r='${rFilter}':g='${gFilter}':b='${bFilter}'`;

    return lutFilter;
    }

    const hexColorsToExclude = ["008000", "007ACC"];
    const lutFilter = generateLutFilter(hexColorsToExclude);

    const ffmpegCommand = `ffmpeg -i input.mp4 -vf "${lutFilter}" -c:a copy output.mp4`;
    console.log(ffmpegCommand);