Skip to content

Instantly share code, notes, and snippets.

@ikariiin
Last active June 27, 2020 13:55
Show Gist options
  • Save ikariiin/795c7ad8bb24f211d985d0a3f18c79e0 to your computer and use it in GitHub Desktop.
Save ikariiin/795c7ad8bb24f211d985d0a3f18c79e0 to your computer and use it in GitHub Desktop.

Revisions

  1. ikariiin revised this gist Jun 27, 2020. 1 changed file with 44 additions and 1 deletion.
    45 changes: 44 additions & 1 deletion readme.md
    Original file line number Diff line number Diff line change
    @@ -10,4 +10,47 @@ change FFMPEG_PATH and FFPROBE_PATH in worker.js to the path you have in your sy

    `npm i fluent-ffmpeg`

    `node index.js`
    `node index.js`

    # expected out put
    ```
    Percentage processing: 10%
    Generating screenshots for job#test
    Percentage processing: 20%
    Percentage processing: 30%
    Percentage processing: 40%
    Percentage processing: 50%
    Percentage processing: 60%
    Percentage processing: 70%
    Percentage processing: 80%
    Percentage processing: 90%
    Percentage processing: 100%
    [
    'F:\\TempEncoderStorage\\screenshot-test-10.png',
    'F:\\TempEncoderStorage\\screenshot-test-20.png',
    'F:\\TempEncoderStorage\\screenshot-test-30.png',
    'F:\\TempEncoderStorage\\screenshot-test-40.png',
    'F:\\TempEncoderStorage\\screenshot-test-50.png',
    'F:\\TempEncoderStorage\\screenshot-test-60.png',
    'F:\\TempEncoderStorage\\screenshot-test-70.png',
    'F:\\TempEncoderStorage\\screenshot-test-80.png',
    'F:\\TempEncoderStorage\\screenshot-test-90.png',
    'F:\\TempEncoderStorage\\screenshot-test-100.png'
    ]
    ```
    # actual output
    ```
    Percentage processing: 10%
    Generating screenshots for job#test
    Percentage processing: 20%
    Percentage processing: 30%
    Percentage processing: 40%
    Percentage processing: 50%
    Percentage processing: 60%
    Percentage processing: 70%
    Percentage processing: 80%
    Percentage processing: 90%
    Percentage processing: 100%
    ```

    It seems the `parentPort.postMessage` is never executed.
  2. ikariiin created this gist Jun 27, 2020.
    13 changes: 13 additions & 0 deletions index.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    import { Worker } from "worker_threads";
    function run() {
    const worker = new Worker("./worker.js", {
    workerData: {
    file: "E:\\TV Series\\Hyouka (720p)\\01 - The Revival of the Long-established Classic Literature Club.mkv",
    storageFolder: "F:\\TempEncoderStorage"
    }
    });

    worker.on("message", (message) => console.log(message));
    }

    run();
    13 changes: 13 additions & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    # Instructions to repro

    create both the files, index.js and worker.js

    change the file name in index.js as required with any video file you want

    change the storage folder as well if you want

    change FFMPEG_PATH and FFPROBE_PATH in worker.js to the path you have in your system idk

    `npm i fluent-ffmpeg`

    `node index.js`
    46 changes: 46 additions & 0 deletions worker.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    const { parentPort, isMainThread, workerData } = require("worker_threads");
    const Ffmpeg = require("fluent-ffmpeg");
    process.env.FFPROBE_PATH="H:\\ffmpeg-20200617-0b3bd00-win64-static\\bin\\ffprobe.exe"
    process.env.FFMPEG_PATH="H:\\ffmpeg-20200617-0b3bd00-win64-static\\bin\\ffmpeg.exe"

    if(isMainThread) {
    process.exit();
    }
    const PERCENTAGE_INCREMENT = 10;

    function execScreenshot(id, percentage) {
    return new Promise((resolve, reject) => {
    console.log(`Percentage processing: ${percentage}%`)
    if(percentage >= PERCENTAGE_INCREMENT * 10) {
    resolve(
    (new Array(10)).fill(0).map((v, k) => `${workerData.storageFolder}\\screenshot-${id}-${k + 1}0.png`)
    );
    return;
    }

    Ffmpeg(workerData.file)
    .on("start", () => percentage <= PERCENTAGE_INCREMENT && console.log(`Generating screenshots for job#${id}`))
    .on("error", (error, stdout, stderr) => {
    console.error(error, stdout, stderr);
    reject(error);
    })
    .on("end", (stdout, stderr) => {
    execScreenshot(id, percentage + PERCENTAGE_INCREMENT);
    })
    .takeScreenshots({
    filename: `screenshot-${id}-${percentage}.png`,
    folder: workerData.storageFolder,
    timemarks: [`${percentage}%`],
    count: 1
    }, workerData.storageFolder);
    })
    }

    async function threadRun() {
    try {
    execScreenshot("test", 10).then(data => parentPort.postMessage(data));
    } catch (e) {
    console.error(e);
    }
    }
    threadRun();