Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

46
LINES

< > BotCompany Repo | #1032316 // DownloadAudio

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (11910L/73K).

sclass DownloadAudio {
  S url;
  File dir = downloadedAudioDir();
  new LS options;
  S extension = ".mp3"; // used to have .opus but got bad quality sometimes
  int maxFileSizeInMB = 20;
  
  S cmd;
  
  *(WithURL<?> url) {
    this(url?.url());
  }
  
  *(S *url) {
    options.add("-U --no-playlist --no-mtime --write-description --write-info-json");
    options.add("--write-annotations --write-thumbnail --print-json --restrict-filenames";
    options.add("-x"); // audio only
    options.add("--audio-format " + dropDotPrefix(extension)); // demand our format
  }
  
  DownloadedMedia get() {
    if (empty(url)) null;
    
    installFFMPEGIfMissing();
    
    options.add("--max-filesize " + maxFileSizeInMB + "m");
    
    S fn = md5FileName(url);
    File infoFile = newFile(dir, fn + ".info");
    S json = loadTextFile(infoFile);
    if (isPossibleJSONMap(json)) {
      DownloadedMedia dm = new(null, json, extension);
      if (fileExists(dm!)) ret dm;
    }
    
    saveTextFile(newFile(dir, fn + ".url"), url);
    
    cmd = pqO(installYouTubeDL())
      + " " + joinWithSpace(options)
      + " -o " + pqO(newFile(dir, youTubeDLDefaultFileNameTemplate()))
      + " " + safePlatformQuote(url);
    S output = backtick_verbose(cmd);
    saveTextFile(infoFile, output);
    ret new DownloadedMedia(cmd, output, extension);
  }
}

Author comment

Began life as a copy of #1032309

download  show line numbers  debug dex  old transpilations   

Travelled to 5 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj, pyentgdyhuwx

No comments. add comment

Snippet ID: #1032316
Snippet name: DownloadAudio
Eternal ID of this version: #1032316/10
Text MD5: e5b6a9a9857e75a2c08c5d0e13a10558
Transpilation MD5: 8857cfedb57f72e9025077946a3ef183
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-09-02 03:00:53
Source code size: 1393 bytes / 46 lines
Pitched / IR pitched: No / No
Views / Downloads: 102 / 226
Version history: 9 change(s)
Referenced in: [show references]