JDownloader Community - Appwork GmbH
 

Reply
 
Thread Tools Display Modes
  #1  
Old 28.11.2024, 17:01
FBD's Avatar
FBD FBD is offline
Giga Loader
 
Join Date: Nov 2018
Location: https://web.libera.chat/#jDownloader
Posts: 96
Post Convert downloaded audio files to mp3 format

This script converts downloaded audio files to mp3 format and removes the original file.

This script requires the "A download stopped" trigger to be selected.

Be default AAC M4A and OGG audio files are converted, add more file extensions if necessary.

Spoiler:
Code:
// Convert audio files to mp3 format
// Trigger : A download stopped

if (link.finished) {
  var input = link.downloadPath;
  // add or remove filetypes here
  var output = input.replace(/(aac|m4a|ogg)$/, "mp3");

  if (input != output) {
      try {
          var ffmpeg = callAPI("config", "get", "org.jdownloader.controlling.ffmpeg.FFmpegSetup", null, "binarypath");
          var bitrate = callSync(ffmpeg, "-i", input).match(/bitrate: (\d+) kb/)[1];

          callAsync(function(error) {
              !error && getPath(input).delete();
          }, ffmpeg, "-y", "-i", input, "-b:a", bitrate + "k", output);
      } catch (e) {};
  }
}


Direct download: raw.githubusercontent.com/FarBeyondDriven/JDscripts/refs/heads/main/ConvertToMP3.js
__________________
irc.libera.chat #jDownloader web.libera.chat/#jDownloader
Reply With Quote
  #2  
Old 07.01.2025, 13:37
pspzockerscene's Avatar
pspzockerscene pspzockerscene is offline
Community Manager
 
Join Date: Mar 2009
Location: Deutschland
Posts: 73,753
Default

Extended version I created for a ticket user:

Main differences:
- special logging capabilities
- exception-test boolean
- all "settings" (boolean switches) are at the beginning of the code
- uses "early return" principle
Code:
Code:
function run() {
    // Converts aac/m4a/ogg/opus files to mp3.
    // Trigger: "A Download Stopped"
    // Requires ffmpeg/ffprobe. Uses ffmpeg/ffprobe settings if available.
    // Does NOT overwrite the destination file (mp3) if it already exists.

    var testExceptionHandling = false; // Boolean to trigger a test exception
    var writeErrorToLogfile = true; // Boolean to determine if errors should be written to a log file
	var deleteSourceFile = true; // Set whether the source file should be deleted after conversion.

    try {
        if (!link.isFinished()) {
            throw new Error("Download is not finished yet"); // Throw error if the download is not completed.
        }

        // Extract file name and extension (as provided)
        var fileName = link.name.replace(/(.+)(\..+$)/, "$1");
        var fileType = link.name.replace(/(.+)(\..+$)/, "$2");
        var sourceFile = link.getDownloadPath();

        // Check if the file is an audio file (aac/m4a/ogg/opus)
        var isAudioFile = /\.(aac|m4a|ogg|opus)$/i.test(sourceFile);
        if (!isAudioFile) {
            return; // Exit the function if it is not an audio file.
        }

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

        // Check if the destination file already exists
        if (getPath(destFile).exists()) {
            throw new Error("The destination file already exists"); // Throw error if the destination file already exists.
        }

        // Retrieve ffmpeg and ffprobe paths (unchanged)
        var ffmpeg = callAPI("config", "get", "org.jdownloader.controlling.ffmpeg.FFmpegSetup", null, "binarypath");
        var ffprobe = callAPI("config", "get", "org.jdownloader.controlling.ffmpeg.FFmpegSetup", null, "binarypathprobe");

        // ffprobe call with "-v quiet" (as provided)
        var data = JSON.parse(callSync(ffprobe, "-v", "quiet", "-print_format", "json", "-show_streams", "-show_format", sourceFile));
        if (!data || !data.streams || data.streams.length === 0) {
            throw new Error("No valid data could be retrieved from the source file"); // Throw error if no valid data is found.
        }

        if (testExceptionHandling) {
            throw new Error("Test-Exception wurde manuell ausgelöst"); // Throw test exception if the flag is true
        }

        // Calculate bitrate (as provided)
        var streamsBitrate = data.streams[0].bit_rate ? data.streams[0].bit_rate : 0;
        var formatBitrate = data.format.bit_rate ? data.format.bit_rate : 0;
        var bitrate = Math.max(streamsBitrate, formatBitrate) / 1000; // Convert bitrate to Kbps

        // Throw error if the bitrate is invalid
        if (bitrate <= 0) {
            throw new Error("The file's bitrate is invalid");
        }

        // Convert the file with ffmpeg
        callSync(ffmpeg, "-y", "-i", sourceFile, "-b:a", bitrate + "k", destFile);

        // Delete the source file if the conversion was successful
        if (deleteSourceFile && getPath(destFile).exists()) {
            deleteFile(sourceFile, false);
        }

    } catch (e) {
        // Check if the destination file path was determined
        if (writeErrorToLogfile && typeof destFile !== 'undefined') {
            // Create a log file and write the exception text into it
            var logFilePath = package.getDownloadFolder() + "/" + fileName + ".txt";
            var text = e.name + ": " + e.message;
            writeFile(logFilePath, text, true);
        }

        // Re-throw the exception to propagate it further
        throw e;
    }
}

// Run the function
run();
__________________
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:49.
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.