View Single Post
  #1174  
Old 07.03.2020, 06:35
zreenmkr zreenmkr is offline
JD Addict
 
Join Date: Feb 2020
Posts: 174
Default

Quote:
Is it possible if you download a video (MP4, MPEG, MKV) from a website (not youtube), to automatically extract the audio part of the video container and then delete the video? Also, is it possible to write a 2nd version of that script that includes also automatic re-encoding with lower bitrate (64kbps?) to drastically shrink the filesize of the audio file?
Adapted from @mgpai

Code:
// Extract Audio from Video then Convert to <custom> mp3 bitrate.
// Trigger required: "A Download Stopped".
// Requires ffmpeg/ffprobe. Uses JD ffmpeg/ffprobe settings if available.
// Overwrites destination file (mp3) if it already exists.


disablePermissionChecks(); //no prompting dialog
//enablePermissionChecks(); //required prompting permision

//setDisableOnException(myBoolean); // enable/disable script on exceptions
//setNotifyOnException(myBoolean); // enable/disable notification on exceptions


if (link.isFinished()) {
    var fileName = link.name.replace(/(.+)(\..+$)/, "$1");
    var fileType = link.name.replace(/(.+)(\..+$)/, "$2");
    var sourceFile = link.getDownloadPath();
    var videoFile = /\.(mp4|mkv|mov|avi|mpg|mpeg)$/.test(sourceFile);

    if (videoFile) {
        var downloadFolder = package.getDownloadFolder();
        var videoSourceFile = sourceFile;
        var audioDestFile = downloadFolder + "/" + fileName + ".mp3";


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

        //---------------------------------------------------------------
        //---extract & convert video to audio
        //---ffmpeg ref cmd: hxxps://gist.github.com/protrolium/e0dbd4bb0f1a396fcb55
        //---ffmpeg -i video.mp4 -f mp3 -ab 192000 -vn music.mp3
        //---------------------------------------------------------------

        var bitrate = 64000;
        //option 1 - CallSync
        callSync(ffmpeg, "-i", videoSourceFile, '-f', 'mp3', '-ab', bitrate, '-vn', audioDestFile);

        //Option 2 - callAsync
        //callAsync(function() {}, ffmpeg, '-i', videoSourceFile, '-f', 'mp3', '-ab', bitrate, '-vn', audioDestFile);

        var deleteSourceFile = false; // Set this to true to delete source file after conversion.
        if (deleteSourceFile && getPath(audioDestFile).exists()) deleteFile(videoSourceFile, false);

    }
}
Reply With Quote