JDownloader Community - Appwork GmbH
 

Reply
 
Thread Tools Display Modes
  #1  
Old 18.02.2025, 19:09
pspzockerscene's Avatar
pspzockerscene pspzockerscene is online now
Community Manager
 
Join Date: Mar 2009
Location: Deutschland
Posts: 74,394
Default Convert .webp/.avif images to .jpg imagee

Convert .webp images to .jpg/.jpeg images

Code:
Code:
    // Converts webm files to jpg.
    // Trigger: "A Download Stopped"
    // Requires ffmpeg/ffprobe. Uses ffmpeg/ffprobe settings if available.
    // Does NOT overwrite the destination file if it already exists.
function run() {
    var deleteSourceFile = true;
    var writeErrorToLogfile = true;
    var changeFilenameInJDownloaderOnSuccess = true;

    try {
        if (!link.isFinished()) {
            // This may sometimes happen (race condition) -> Early return without exception
            // throw new Error("Download is not finished yet");
            return;
        }

        var fileName = link.name.replace(/(.+)(\..+$)/, "$1");
        var sourceFile = link.getDownloadPath();

        // Ensure the file is a .webp image
        if (!/\.webp$/i.test(sourceFile)) {
            return;
        }

        var downloadFolder = package.getDownloadFolder();
        var destFile = downloadFolder + "/" + fileName + ".jpg";

        // Prevent overwriting an existing file
        if (getPath(destFile).exists()) {
            throw new Error("The destination file already exists");
        }

        var ffmpeg = callAPI("config", "get", "org.jdownloader.controlling.ffmpeg.FFmpegSetup", null, "binarypath");
        callSync(ffmpeg, "-y", "-i", sourceFile, "-q:v", "1", destFile);

        // Ensure the destination file was created successfully
        if (!getPath(destFile).exists()) {
            throw new Error("File conversion failed, destination file not found");
        }

        // Delete the source file if conversion was successful
        if (deleteSourceFile) {
            deleteFile(sourceFile, false);
        }

        // Change filename in JDownloader if enabled
        if (changeFilenameInJDownloaderOnSuccess) {
            link.setName(fileName + ".jpg");
        }
    } catch (e) {
        if (writeErrorToLogfile) {
            var logFilePath = package.getDownloadFolder() + "/" + fileName + ".txt";
            writeFile(logFilePath, e.name + ": " + e.message, true);
        }
        throw e;
    }
}

// Execute the function
run();
Context/source:
https://board.jdownloader.org/showth...497#post544497
__________________
JD Supporter, Plugin Dev. & Community Manager

Erste Schritte & Tutorials || JDownloader 2 Setup Download
Spoiler:

A users' JD crashes and the first thing to ask is:
Quote:
Originally Posted by Jiaz View Post
Do you have Nero installed?

Last edited by pspzockerscene; 19.02.2025 at 12:03.
Reply With Quote
  #2  
Old 19.02.2025, 12:04
pspzockerscene's Avatar
pspzockerscene pspzockerscene is online now
Community Manager
 
Join Date: Mar 2009
Location: Deutschland
Posts: 74,394
Default

Updated script:
- outsourced "if file exist after conversion" check
- added boolean (default true) that allows for changing file name in JDownloader GUI too on successful conversion
__________________
JD Supporter, Plugin Dev. & Community Manager

Erste Schritte & Tutorials || JDownloader 2 Setup Download
Spoiler:

A users' JD crashes and the first thing to ask is:
Quote:
Originally Posted by Jiaz View Post
Do you have Nero installed?
Reply With Quote
  #3  
Old 18.03.2025, 00:00
Bear Bear is offline
Modem User
 
Join Date: Mar 2025
Posts: 3
Post Convert .avif images to .jpg/.jpeg images

Hi there!

This script works great, but.....I have one (similar) request.
Can you make for me a script that converts .avif images too?

Tnx in advance!
Reply With Quote
  #4  
Old 18.03.2025, 09:03
pspzockerscene's Avatar
pspzockerscene pspzockerscene is online now
Community Manager
 
Join Date: Mar 2009
Location: Deutschland
Posts: 74,394
Default

@Bear
A little initiative would be more than appropriate here.
Have you already looked at the script I posted, adapted it to your target format, and tested it?
Have you studied the topic of EventScripter in more detail?
EventScripter subforum:
https://board.jdownloader.org/forumdisplay.php?f=52
EventScripter help article:
https://support.jdownloader.org/know...event-scripter
Just a tip: ChatGPT can also be helpful in this regard.
__________________
JD Supporter, Plugin Dev. & Community Manager

Erste Schritte & Tutorials || JDownloader 2 Setup Download
Spoiler:

A users' JD crashes and the first thing to ask is:
Quote:
Originally Posted by Jiaz View Post
Do you have Nero installed?
Reply With Quote
  #5  
Old 18.03.2025, 11:55
Bear Bear is offline
Modem User
 
Join Date: Mar 2025
Posts: 3
Default

Hi!
Sorry, my bad that I didn't mentioned what I tried.
I've looked your script before posting request and adapted to my needs.
That is line 21, where is testing file extension. I've changed to .avif but it's
not working. Getting error message: "Error: File conversion failed, destination
file not found (#55)
"
Trigger -> A Download Stopped

Din't used ChatGPT before, but now I had opportunity to test it (thanks for the tip)
It says the same thing. Changing webp to avif.
ffmpeg is correctly installed, can handle avif files (tried manually convert file
from cmd and it works)
Because of error which says that destination file is not found and it's downloaded, I'm stuck
here, and I was thinking that there is more in code what needs to be changed and not
only webp to avif. I hope you understand me.
Any ideas?

You have some detailed infos about error if it helps: (This error is caused by:)
Spoiler:
net.sourceforge.htmlunit.corejs.javascript.JavaScriptException: Error: File conversion failed, destination file not found (#55)
at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpretLoop(Interpreter.java:1006)
at script.run(:55)
at script(:60)
at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpret(Interpreter.java:798)
at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.call(InterpretedFunction.java:105)
at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.doTopCall(ContextFactory.java:411)
at org.jdownloader.scripting.JSHtmlUnitPermissionRestricter$SandboxContextFactory.doTopCall(JSHtmlUnitP ermissionRestricter.java:134)
at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3286)
at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.exec(InterpretedFunction.java:115)
at net.sourceforge.htmlunit.corejs.javascript.Context.evaluateString(Context.java:1361)
at org.jdownloader.extensions.eventscripter.ScriptThread.evalUNtrusted(ScriptThread.java:346)
at org.jdownloader.extensions.eventscripter.ScriptThread.executeScipt(ScriptThread.java:194)
at org.jdownloader.extensions.eventscripter.ScriptThread.run(ScriptThread.java:174)

Thanks!
Reply With Quote
  #6  
Old 18.03.2025, 12:25
pspzockerscene's Avatar
pspzockerscene pspzockerscene is online now
Community Manager
 
Join Date: Mar 2009
Location: Deutschland
Posts: 74,394
Default

I need the full script/code you've triied otherwise I won't be able to help.
Also please provide example links to .avif image files.
__________________
JD Supporter, Plugin Dev. & Community Manager

Erste Schritte & Tutorials || JDownloader 2 Setup Download
Spoiler:

A users' JD crashes and the first thing to ask is:
Quote:
Originally Posted by Jiaz View Post
Do you have Nero installed?
Reply With Quote
  #7  
Old 18.03.2025, 13:25
pspzockerscene's Avatar
pspzockerscene pspzockerscene is online now
Community Manager
 
Join Date: Mar 2009
Location: Deutschland
Posts: 74,394
Default

So here is my test results:
Indeed that didn't work out of the box but it wasn't "ChatGPTs fault":
The FFmpeg version that came with JDownloader didn't support avif.
That can be tested via this command:
Code:
ffmpeg -formats | findstr avif
-> It should return "E avif" or "D avif" in the last line.

Solution:
1. Grab another FFmpeg build here:
ffmpeg.org/download.html

2. Make sure that that supports avif.

3. Either install it on your system or replace it in your JD install folder, see:
https://support.jdownloader.org/know...roubleshooting

4. Use the .avif to .jpg script modified via ChatGPT:
Script:
Code:
    // Converts avif files to jpg.
    // Trigger: "A Download Stopped"
    // Requires ffmpeg/ffprobe. Uses ffmpeg/ffprobe settings if available.
    // Does NOT overwrite the destination file if it already exists.
function run() {
    var deleteSourceFile = true;
    var writeErrorToLogfile = true;
    var changeFilenameInJDownloaderOnSuccess = true;

    try {
        if (!link.isFinished()) {
            // This may sometimes happen (race condition) -> Early return without exception
            // throw new Error("Download is not finished yet");
            return;
        }

        var fileName = link.name.replace(/(.+)(\..+$)/, "$1");
        var sourceFile = link.getDownloadPath().replace(/\//g, "\\");

        // Ensure the file is a .avif image
        if (!/\.avif$/i.test(sourceFile)) {
            return;
        }

        var downloadFolder = package.getDownloadFolder().replace(/\//g, "\\");
        var destFile = downloadFolder + "\\" + fileName + ".jpg";

        // Prevent overwriting an existing file
        if (getPath(destFile).exists()) {
            throw new Error("The destination file already exists");
        }

        var ffmpeg = callAPI("config", "get", "org.jdownloader.controlling.ffmpeg.FFmpegSetup", null, "binarypath").replace(/\//g, "\\");
        callSync(ffmpeg, "-y", "-i", sourceFile, "-q:v", "1", destFile);

        // Ensure the destination file was created successfully
        if (!getPath(destFile).exists()) {
            throw new Error("File conversion failed, destination file not found: " + destFile);
        }

        // Delete the source file if conversion was successful
        if (deleteSourceFile) {
            deleteFile(sourceFile, false);
        }

        // Change filename in JDownloader if enabled
        if (changeFilenameInJDownloaderOnSuccess) {
            link.setName(fileName + ".jpg");
        }
    } catch (e) {
        if (writeErrorToLogfile) {
            var logFilePath = package.getDownloadFolder().replace(/\//g, "\\") + "\\" + fileName + ".txt";
            writeFile(logFilePath, e.name + ": " + e.message, true);
        }
        throw e;
    }
}

// Execute the function
run();
Prompt I entered to get this:
Code:
Change the following script so that it converts .avif files to .jpg:
In the comments, only change the bare minimum e.g. all mentions of the old .webp to .jpg functionality.
Example link I used for testing:
Code:
libre-software.net/wp-content/uploads/AVIF/AVIF%20Test%20picture%20%28JPEG%20converted%20to%20AVIF%20with%20Convertio%29.avif
__________________
JD Supporter, Plugin Dev. & Community Manager

Erste Schritte & Tutorials || JDownloader 2 Setup Download
Spoiler:

A users' JD crashes and the first thing to ask is:
Quote:
Originally Posted by Jiaz View Post
Do you have Nero installed?
Reply With Quote
  #8  
Old 18.03.2025, 13:44
Bear Bear is offline
Modem User
 
Join Date: Mar 2025
Posts: 3
Thumbs up

Thank you very much!
It's working now!
I've replaced FFmpeg with new one in JD install folder and now it's working fine!

Regards!
Reply With Quote
  #9  
Old 18.03.2025, 14:28
pspzockerscene's Avatar
pspzockerscene pspzockerscene is online now
Community Manager
 
Join Date: Mar 2009
Location: Deutschland
Posts: 74,394
Default

Thanks for your feedback.
__________________
JD Supporter, Plugin Dev. & Community Manager

Erste Schritte & Tutorials || JDownloader 2 Setup Download
Spoiler:

A users' JD crashes and the first thing to ask is:
Quote:
Originally Posted by Jiaz View Post
Do you have Nero installed?
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT +2. The time now is 17:09.
Provided By AppWork GmbH | Privacy | Imprint
Parts of the Design are used from Kirsch designed by Andrew & Austin
Powered by vBulletin® Version 3.8.10 Beta 1
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.