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).

1  
sclass DownloadAudio {
2  
  S url;
3  
  File dir = downloadedAudioDir();
4  
  new LS options;
5  
  S extension = ".mp3"; // used to have .opus but got bad quality sometimes
6  
  int maxFileSizeInMB = 20;
7  
  
8  
  S cmd;
9  
  
10  
  *(WithURL<?> url) {
11  
    this(url?.url());
12  
  }
13  
  
14  
  *(S *url) {
15  
    options.add("-U --no-playlist --no-mtime --write-description --write-info-json");
16  
    options.add("--write-annotations --write-thumbnail --print-json --restrict-filenames";
17  
    options.add("-x"); // audio only
18  
    options.add("--audio-format " + dropDotPrefix(extension)); // demand our format
19  
  }
20  
  
21  
  DownloadedMedia get() {
22  
    if (empty(url)) null;
23  
    
24  
    installFFMPEGIfMissing();
25  
    
26  
    options.add("--max-filesize " + maxFileSizeInMB + "m");
27  
    
28  
    S fn = md5FileName(url);
29  
    File infoFile = newFile(dir, fn + ".info");
30  
    S json = loadTextFile(infoFile);
31  
    if (isPossibleJSONMap(json)) {
32  
      DownloadedMedia dm = new(null, json, extension);
33  
      if (fileExists(dm!)) ret dm;
34  
    }
35  
    
36  
    saveTextFile(newFile(dir, fn + ".url"), url);
37  
    
38  
    cmd = pqO(installYouTubeDL())
39  
      + " " + joinWithSpace(options)
40  
      + " -o " + pqO(newFile(dir, youTubeDLDefaultFileNameTemplate()))
41  
      + " " + safePlatformQuote(url);
42  
    S output = backtick_verbose(cmd);
43  
    saveTextFile(infoFile, output);
44  
    ret new DownloadedMedia(cmd, output, extension);
45  
  }
46  
}

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: 169 / 317
Version history: 9 change(s)
Referenced in: [show references]