ahmadu suleiman
ahmadu suleiman

Reputation: 71

How to download and add subtitles to video using Exoplayer

I have a video player app which used the Exoplayer library to play videos but I want to be able to download and add subtitles dynamically using something like OpenSubtitles and then add them to the currently playing video.

Video players with similar features are MX Player and X Player.

How can I be able to achieve a similar functionality. Your answers are highly appreciated!

Upvotes: 2

Views: 1191

Answers (2)

Usman click
Usman click

Reputation: 11

Yes, actually. OpenSubtitles.org has a nice API for searching and downloading.

Here you go https://opensubtitles.stoplight.io/docs/opensubtitles-api/e3750fd63a100-getting-started

Upvotes: 1

Nermeen
Nermeen

Reputation: 15973

You can download subtitles separately or checking downloading media and add them using sideloading subtitles or you can use SingleSampleMediaSource like the following:

    MediaSource[] mediaSources = new MediaSource[subtitlesCount + videoTrack];  
    mediaSources[0] = new ProgressiveMediaSource.Factory(new FileDataSource.Factory())
                        .createMediaSource(MediaItem.fromUri(videoPath));
//add subtitles tracks, and use correct format
    SingleSampleMediaSource.Factory singleSampleSourceFactory = new SingleSampleMediaSource.Factory(new DefaultDataSourceFactory(context));
    for (int i = 0; i < videoInfo.getSubtitlePaths().size(); i++) {
        mediaSources[i + videoTrack] = singleSampleSourceFactory.createMediaSource(
                    new MediaItem.Subtitle(Uri.parse(subtitlePath), MimeTypes.TEXT_VTT, language), C.TIME_UNSET);
    }
//use MergingMediaSource to combine the media sources
    MergingMediaSource mergedSource = new MergingMediaSource(mediaSources);
....init player
mPlayer.addMediaSource(mergedSource);
mPlayer.prepare();

Upvotes: 1

Related Questions