Marium Nasir
Marium Nasir

Reputation: 13

How to save the filename of youtube video downloaded from youtube-dl in a variable

I am making an android app which extracts audio from youtube using youtube-dll. I want to save the title of that youtube video in a variable but command request.option("--get-title") is not returning anything and I don't know how to save it in a variable. Can someone please help. This is the code

    YoutubeDLRequest request = new YoutubeDLRequest(url);
    File youtubeDLDir = getDownloadLocation();
    request.setOption("-o", youtubeDLDir.getAbsolutePath() + "/%(title)s.% 
    (ext)s");
    request.setOption("-x");
    request.setOption("--prefer-ffmpeg");
    request.setOption("--add-metadata");
    request.setOption("--metadata-from-title", "%(artist)s - %(title)s");
    request.setOption("--embed-thumbnail");
    request.setOption("--audio-format", "mp3");

Upvotes: 1

Views: 1830

Answers (2)

Mahmood Hussain
Mahmood Hussain

Reputation: 501

This snippet will give you Title and Extension of Video

String[] getVideoInfo(String url) throws YoutubeDLException, InterruptedException {
    VideoInfo streamInfo = YoutubeDL.getInstance().getInfo(url);
    return new String[]{streamInfo.getTitle(), streamInfo.getExt()};
}

then access it like this

 String title = getVideoInfo(url)[0];
 String extension = getVideoInfo(url)[1];

Upvotes: 0

csabinho
csabinho

Reputation: 1609

You'll have to use the static YoutubeDL.getVideoInfo(String url)-method. Afterwards you can either save it into a VideoInfo-instance or use the member variable directly from the return value.

Upvotes: 1

Related Questions