ExoPlayer icon indicating copy to clipboard operation
ExoPlayer copied to clipboard

Support for .srt subtitle format

Open aravinds03 opened this issue 11 years ago • 33 comments

Hi, Is there any support for srt subtitle parser in exoplayer? If yes, can you please provide example code snippet? If no, can you please provide alternate solution?

aravinds03 avatar Jun 29 '15 07:06 aravinds03

SRT is supported, but the setup is a little convoluted. I'm working on making it at least a little bit simpler, and will update this bug in a couple of days once the necessary changes are pushed.

ojw28 avatar Jun 30 '15 22:06 ojw28

Is there any example of SRT implementation? I'm trying to work with the one for WebVTT in #39, but I seem to be missing the proper knowledge of working with the API, and also things seem to be slightly changed since that example. Thanks!

andreimarinescu avatar Jul 20 '15 10:07 andreimarinescu

Sorry for the delay in simplifying this. We're getting closer.

ojw28 avatar Jul 21 '15 16:07 ojw28

Hi, Any update here?

aravinds03 avatar Jul 27 '15 01:07 aravinds03

On the dev branch, you should now be able to load and play a subtitle file like:

DataSource textDataSource = new DefaultUriDataSource(context, bandwidthMeter, userAgent);
SingleSampleSource textSampleSource = new SingleSampleSource(textUri, textDataSource,
    MediaFormat.createTextFormat(MimeTypes.APPLICATION_SUBRIP, C.MATCH_LONGEST_US));
TrackRenderer textRenderer = new TextTrackRenderer(textSampleSource, player,
    player.getMainHandler().getLooper());

ojw28 avatar Jul 29 '15 21:07 ojw28

Hi, Is this change released in 1.4.2 ?

aravinds03 avatar Aug 12 '15 05:08 aravinds03

Yes.

ojw28 avatar Aug 12 '15 08:08 ojw28

How to give the path of SRT file to exoplayer.? Is there any demo code for use it? On 12 Aug 2015 13:48, "ojw28" [email protected] wrote:

Yes.

— Reply to this email directly or view it on GitHub https://github.com/google/ExoPlayer/issues/587#issuecomment-130213732.

sachinbhanot avatar Aug 12 '15 08:08 sachinbhanot

textUri in the sampe code above is the path of the file. Have you tried using that code? The change also shows you which branches and release tags it's in, if you click on it.

ojw28 avatar Aug 12 '15 08:08 ojw28

Actually I m following this thread long time... Never tried yet... I have subtitle file downloaded from internet. I want to use with my HLS stream ... So I will try it and let u know thanks On 12 Aug 2015 13:57, "ojw28" [email protected] wrote:

textUri in the sampe code above is the path of the file. Have you tried using that code? The change also shows you which branches and release tags it's in, if you click on it.

— Reply to this email directly or view it on GitHub https://github.com/google/ExoPlayer/issues/587#issuecomment-130215115.

sachinbhanot avatar Aug 12 '15 08:08 sachinbhanot

@ojw28 I have a timed text in a non standard format, I have parsed this into subrip format. Now I don't want to write this subrip to disk, instead directly use it in the ExoPlayer. I tried using ByteArrayDataSource with the subrip string and SingleSampleSource with Uri.Empty. This doesn't seem to work. any pointers?

pratikjk avatar Aug 12 '15 12:08 pratikjk

Hi @ojw28, I am using this SingleSampleSource class to load a TTML subtitle file, it works except for one thing, it's related to that #707 issue, not getting STATE_ENDED when the video is finished.

This is using the code from the tag r1.4.2.

In TextTrackRenderer's doSomeWork(), it's calling readData() on the sample source, and will set inputStreamEnded to true only if readData() return SampleSource.END_OF_STREAM. And inside SingleSampleSource's readData() implementation, it will return END_OF_STREAM only if pendingSample is true. This flag pendingSample will be set to true when seekToUs() is called, that's why whenever we seek, the video will never get back to STATE_ENDED state.

Wonder if there's an easy solution for this problem.

Kening avatar Aug 13 '15 15:08 Kening

END_OF_STREAM is sent if pendingSample is false, not if it's true...?

ojw28 avatar Aug 13 '15 15:08 ojw28

my typo, END_OF_STREAM is sent if pendingSample is false.

pendingSample will be set to true after enable() is called (it will be set to true also when seekToUs() is called), so actually even without seeking, i will face the same problem since the sample source will be enabled when the TextTrackRenderer is enabled, and I will never have a chance to set it back to false unless I call disable() on it.

Kening avatar Aug 13 '15 15:08 Kening

Ah, I see the problem. I think this is fixed already on the dev branch.

ojw28 avatar Aug 13 '15 15:08 ojw28

I see the change in SingleSampleSource, that should fix this problem, however there is now another problem related to seeking while I pull in dev branch. I am not able to seek properly while playing HLS video, the seekbar and progress indicator is no longer working.

And if I run ExoPlayer's demo app, I see similar issue, when I play HLS video in the ExoPlayer demo (any video in the HLS section), the media controller is not working properly, don't see any progress on the seekbar and the position is not updating, seeking is not working. Maybe a known issue, or maybe another issue to track it?

Kening avatar Aug 13 '15 15:08 Kening

@ojw28 I am able to pull the change in SingleSampleSource from dev branch and apply just that on top of r1.4.2.

I run into one problem, in SingleSampleSource.java there's this line:

private void maybeStartLoading() { if (loadingFinished || state != STATE_END_OF_STREAM || loader.isLoading()) return; .... }

the check of state != STATE_END_OF_STREAM is preventing loader.startLoading from happening, since the state is not initialized to STATE_END_OF_STREAM but 0, which is STATE_SEND_FORMAT, so I add a line:

if (loadingFinished || (state != STATE_END_OF_STREAM && state != STATE_SEND_FORMAT) || loader.isLoading()) return;

this fix my problem, now I am able to get STATE_ENDED on the player when a video with closed caption is finished playing.

Thanks for your help.

Kening avatar Aug 13 '15 19:08 Kening

Thanks for the feedback.

  • It should be state == STATE_END_OF_STREAM (== rather than !=). I'll get this fixed on the dev branch.
  • Yes, seeking is currently broken for HLS on the dev branch. I'll take a look and get it fixed.

ojw28 avatar Aug 13 '15 19:08 ojw28

The issues on the dev branch should be fixed by the change above. Note that dev is a little unstable in general at the moment, due to work on #514.

ojw28 avatar Aug 13 '15 19:08 ojw28

Thanks for srt support. It worked perfectly. For those who are looking for sample code, here it is,

//text renderer. DataSource textDataSource = new DefaultUriDataSource(context, bandwidthMeter, userAgent); SingleSampleSource textSampleSource = new SingleSampleSource(Uri.parse("http://path.srt"), textDataSource, MediaFormat.createTextFormat( MimeTypes.APPLICATION_SUBRIP, //use "text/vtt" as mimeType for vtt subtitle format. C.MATCH_LONGEST_US)); TrackRenderer textRenderer = new TextTrackRenderer(textSampleSource, player, player.getMainHandler().getLooper());

TrackRenderer[] renderers = new TrackRenderer[DemoPlayer.RENDERER_COUNT]; //..set audio/video renderers. renderers[DemoPlayer.TYPE_TEXT] = textRenderer; player.onRenderers(null, null, renderers, bandwidthMeter);

//text renderer is disabled by default initially. So, enable it after clicking CC button. player.selectTrack(DemoPlayer.TYPE_TEXT, DemoPlayer.PRIMARY_TRACK);

//disable subtitle player.selectTrack(DemoPlayer.TYPE_TEXT, DemoPlayer.DISABLED_TRACK);

aravinds03 avatar Aug 26 '15 16:08 aravinds03