How to download and add subtitles to video using Exoplayer - android

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!

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

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();

Related

Android Exo Player - Trouble playing Video on Demand from API

I'm working with the Pac-12 API (college sports data) and trying to make a streaming video player.
The Pac-12 API (http://api.pac-12.com/v3/vod?page=0") gives back a list of video objects with urls for their Video's on Demand (VODs).
I have exo player set up in Android and it's working for videos with urls that end in .mp4.
But when I try to plug in the VOD urls it's giving an error that it cannot extract the file type.
I'm using exo player 2.8.4 which may or may not be the problem. But the newer version of exo player ( 2.9.0 and above ) has min sdk 26 (which my testing phone doesn't run).
I'm working on checking if that's the problem, but in the mean time I wanted to post this question in case anyone can help.
Here is my exo player set up. mediaUrl is the varaible that works with .mp4 but not the VOD url.
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(
context, Util.getUserAgent(context, "RecyclerView VideoPlayer"));
String mediaUrl = mediaObjects.get(targetPosition).getMedia_url();
if (mediaUrl != null) {
MediaSource videoSource = new ExtractorMediaSource.Factory(dataSourceFactory)
.createMediaSource(Uri.parse(mediaUrl));
videoPlayer.prepare(videoSource);
videoPlayer.setPlayWhenReady(true);
}
The VOD urls give no file type extension at the end, so I tried concatenating '.vod' onto the mediaUrl varaible, but no luck with that.
From reading around online it seems like VODs are supported by exo player, but I can't find much about how the setup might be different.
Heres a direct link to one of the Pac-12 VODs. This is the same url as what the API returns.
https://pac-12.com/videos/oregons-dana-altman-talks-andy-katz-about-recruitment-getting-fans-back-and-more

How to play android motion(Live) photo with MediaPlayer?

I want to play android motion photo with media player. But I don't want to use mediaPlayer.setDataSource(fileDescriptor, offset, length). Because I am not getting offset for Huawei device's Motion Photo. How can I get the offset for all device's motion photo or is it possible to use mediaPlayer.setDataSource(path, headers) instead? I have heard about a header called 'ftypmp42'. How to use this in this case?
You may refer this ,
i have Done something I may Tell you that may help you to Find the Offset and play
XmpUtil xmp = new XmpUtil();
XMPMeta meta = xmp.extractOrCreateXMPMeta(jpegFile.getAbsolutePath());
haveVideoValue = meta.getProperty("http://ns.google.com/photos/1.0/camera/","GCamera:MicroVideo");
offset=meta.getProperty("http://ns.google.com/photos/1.0/camera/","GCamera:MicroVideoOffset");
//To play the Video with Media player
FileInputStream inputStream = new FileInputStream(filePath);
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(inputStream.getFD(), finalJpegFile.length() - Long.valueOf(finalOffset1), Long.valueOf(finalOffset1));
mp.prepare();
inputStream.close();
mp.setDisplay(surface.getHolder());
mp.setLooping(true);
mp.start();
Credit :- https://medium.com/android-news/working-with-motion-photos-da0aa49b50c

Play multiple URL at once in Android

How to play multiple URLs sequentially in android? I have a small streaming url 10s/each of one complete song. I need to play it one after another. Is there any way doing it so the music don't get lagged?
https://exoplayer.dev/media-sources.html read Advanced composition
MediaSource firstSource =
new ProgressiveMediaSource.Factory(...).createMediaSource(firstVideoUri);
MediaSource secondSource =
new ProgressiveMediaSource.Factory(...).createMediaSource(secondVideoUri);
// Plays the first video twice, then the second video.
ConcatenatingMediaSource concatenatedSource =
new ConcatenatingMediaSource(firstSource, firstSource, secondSource);
It looks like you are using the m3u8 playlist to play with MediaPlayer. However, you wouldn't able to do it. You can use ExoPlayer to play m3u8 list. Check this link
https://github.com/google/ExoPlayer
simpleExoPlayerView.setShowMultiWindowTimeBar(true);

how to add multiple subtitles in Exo Player 2

I want to show subtitles with ExoPlayer 2 . users can choose between languages (English,German, or Arabic) . Video links are HLS (.m3u8) and subtitles are .str files .
I couldn't find any samples to do this.
is there any sample?
The link I added as a comment to your original post will be how you'll build the UI around text track selection. Then to actually get the tracks to be added to your mp4 file (or whatever the format is), you'll want to use a MergingMediaSource. The simple version looks like so:
MediaSource videoSource = new ExtractorMediaSource(videoUri, ...);
MediaSource subtitleSource = new SingleSampleMediaSource(subtitleUri, ...);
// Plays the video with the sideloaded subtitle.
MergingMediaSource mergedSource = new MergingMediaSource(videoSource, subtitleSource);
You can merge multiple subtitle tracks into the video source. Many different file formats are accepted.
I got that particular code sample from this blog post - but I believe that same code is also in the ExoPlayer documentation. That code block combined with the sample code that I link to in my other answer here should be enough to get you some subtitles.
Please let me know if that works for you.

Is there a non-youtube example to implement DASH using ExoPlayer?

Hi I am looking for an example to configure ExoPlayer for DASH. But the example I found uses Youtube videos. Is there an example on videos which are not on youtube? Can DASH be configured for any video on the internet?
Yes, ExoPlayer can play any DASH, SmoothStreaming, HLS or MP4 Progressive download over HTTP URLs.
The demo application provided in ExoPlayer source code can be modified to add any video that will be shown in the startup Activity. To do that, edit https://github.com/google/ExoPlayer/blob/master/demo/src/main/java/com/google/android/exoplayer/demo/Samples.java file to add a new sample set.
Example:
public static final Sample[] CUSTOM_DASH_VIDEOS = new Sample[] {
new Sample("Some User friendly name of video 1",
"http://www.somewhere.com/somecontent.mpd?param1=val1&param2=val2", DemoUtil.TYPE_DASH),
new Sample("Some User friendly name of video 2",
"http://www.somewhere.com/somecontent.mpd?param1=val1&param2=val2", DemoUtil.TYPE_DASH),
};
Now, in https://github.com/google/ExoPlayer/blob/master/demo/src/main/java/com/google/android/exoplayer/demo/SampleChooserActivity.java add a new row in sample adapter.
sampleAdapter.add(new Header("Custom DASH Videos"));
sampleAdapter.addAll((Object[]) Samples.CUSTOM_DASH_VIDEOS);
Hope this answers your question.

Categories

Resources