google / ExoPlayer

This project is deprecated and stale. The latest ExoPlayer code is available in https://github.com/androidx/media
https://developer.android.com/media/media3/exoplayer
Apache License 2.0
21.7k stars 6.02k forks source link

How to get whole track selection text as string #8750

Closed aliyazdi75 closed 3 years ago

aliyazdi75 commented 3 years ago

I know we can get the cue text by Objects.requireNonNull(exoPlayer.getTextComponent()).getCurrentCues().get(0).text but how to get the whole subtitle file as a string for a specific text track selection?

icbaker commented 3 years ago

ExoPlayer's subtitle infrastructure is designed to make cues available as their playback position is reached (so that they can be shown on the screen) - you can access this by registering a TextOutput implementation via SimpleExoPlayer#addTextOutput. Note that, depending on the format, cues aren't necessarily textual - they can also be images. So it's not possible in the general case to represent a Cue in a human-readable text form.

I'm not sure quite what you mean by this though:

how to get the whole subtitle file as a string for a specific text track selection?

I assume that either you start off with the subtitles in a separate file already (like something.vtt, something.srt, something.ass, etc) or the subtitles are multiplexed inside a container format like MP4. In the former case it seems your question is trivially answered: just use the text file you're already providing to ExoPlayer. In the latter case, where the subtitles are muxed inside a container, you don't need to use ExoPlayer to extract them, you're probably best off using a tool like ffmpeg instead. The same caveat about image subtitles applies - depending on the subtitle format you may not be able to easily generate a textual representation.

aliyazdi75 commented 3 years ago

Oh, thanks. Yes, we are using the Flutter framework and video player plugin which uses exoplayer as backed, and we need to get the srt subtitle text file inside a container format and represent it as a widget in the flutter application. So, as you mentioned there is no class in the exoplayer that returns the whole srt text? And if not how to pass this multiplexed track selected subtitle to a tool like ffmpeg?

icbaker commented 3 years ago

So, as you mentioned there is no class in the exoplayer that returns the whole srt text?

Correct. When muxing subtitles into a container, the samples are generally split up throughout the file, so when playing we only read the samples slightly ahead of the playback position, meaning ExoPlayer never reads the whole content of the 'complete' srt file until playback is complete.

And if not how to pass this multiplexed track selected subtitle to a tool like ffmpeg?

How to use ffmpeg is outside the scope of this issue tracker - you may get more help somewhere like Stack Overflow.

I would imagine you'd do something like pass the file to ffmpeg and extract all the subtitle tracks to srt files, then switch between these files by track ID by listening to ExoPlayer's Player.EventListener#onTracksChanged() event.

aliyazdi75 commented 3 years ago

Thank you so much for your help.