sealedtx / java-youtube-downloader

Simple, almost zero-dependency java parser for retrieving youtube video metadata
Other
413 stars 115 forks source link

tiktok support #127

Open sealedtx opened 6 months ago

sealedtx commented 6 months ago

I was asked to implement support for downloading videos from tiktok. I've created a separate branch for that purpose. I don't consider it to be merged to master (at least with current implementation), so I'll keep it here for now.

To use it you need to set specific version of library in maven/gradle:

<dependency>
    <groupId>com.github.sealedtx</groupId>
    <artifactId>java-youtube-downloader</artifactId>
    <version>feature-tiktok-support-cb26cd7a9e-1</version>
</dependency>

or

dependencies {
    implementation 'com.github.sealedtx:java-youtube-downloader:feature-tiktok-support-cb26cd7a9e-1'
}

and update code to init YoutubeDownloader differently, but rest of the code will remain same as for youtube videos:

        // NEW PART: init downloader with new TitkotParserImpl
        Config config = Config.buildDefault();
        DownloaderImpl downloader = new DownloaderImpl(config);
        TitkotParserImpl tiktokParser = new TitkotParserImpl(config, downloader, new TiktokExtractorImpl());
        YoutubeDownloader youtubeDownloader = new YoutubeDownloader(config, downloader, tiktokParser);

        // during request video info pass full video link (NOT just id) as videoId
        RequestVideoInfo videoInfoRequest = new RequestVideoInfo("https://www.tiktok.com/@{username}/video/{id}");
        Response<VideoInfo> videoInfoResponse = youtubeDownloader.getVideoInfo(videoInfoRequest);
        VideoInfo videoInfo = videoInfoResponse.data();

        // data format is mostly unchanged, but not all information is present as in youtube video 
        System.out.println(videoInfo.details().title());
        System.out.println(videoInfo.details().viewCount());
        List<Format> formats = videoInfo.formats();
        for (Format format : formats) {
            System.out.println(format.url());

            RequestVideoFileDownload videoDownloadRequest = new RequestVideoFileDownload(format);
            Response<File> downloadedFile = youtubeDownloader.downloadVideoFile(videoDownloadRequest);
            System.out.println("downloaded: " + downloadedFile.data().getName());
        }

If you want to download video by format.url() in other way (not with YoutubeDonwloader), you will need to add cookies to you download request, you can get them from Config instance:

        String cookie = config.getHeaders().get("Cookie");
        System.out.println(cookie);