Skip to content

Subtitles on Internet

Muhammad Hasan Alasady edited this page May 24, 2021 · 1 revision

Internet Subtitles

You have two ways to use subtitles on the internet, firstly using SubtitleController and it's the easly way, secondly by using SubtitleProvider and SubtitleParser, this is both of ways:

  • By SubtitleController:

Firstly provide the link of subtitle (should be an object of Uri), after that you can use SubtitleProvider.fromNetwork(url) or NetworkSubtitle(url):

var url = Uri.parse(
    'https://brenopolanski.github.io/html5-video-webvtt-example/MIB2-subtitles-pt-BR.vtt');
var controller = SubtitleController(
  provider: SubtitleProvider.fromNetwork(url),
);

Or

var url = Uri.parse(
    'https://brenopolanski.github.io/html5-video-webvtt-example/MIB2-subtitles-pt-BR.vtt');
var controller = SubtitleController(
  provider: NetworkSubtitle(url),
);
  • By SubtitleProvider and SubtitleParser:

Like the last one, provide the link and provider, after that create a new object of SubtitleObject and await it to preparing the subtitle data, in the last you can use SubtitleParser to parsing the subtitles:

var url = Uri.parse(
    'https://brenopolanski.github.io/html5-video-webvtt-example/MIB2-subtitles-pt-BR.vtt');

SubtitleProvider provider = NetworkSubtitle(url);
SubtitleObject object = await provider.getSubtitle();
SubtitleParser parser = SubtitleParser(object);     

After creating the controller you have to initialize it by call controller.initial(), it's async function, so you have to await it.

await controller.initial();

What is next?

When you intialize it, you can use this methods to dealing with subtitles using SubtitleController, see the SubtitleController Documentation for more. Check the API Reference for more.

Example

import 'package:subtitle/subtitle.dart';

void main(List<String> args) async {
  //! By using controller - it's easly way
  var url = Uri.parse(
      'https://brenopolanski.github.io/html5-video-webvtt-example/MIB2-subtitles-pt-BR.vtt');
  var controller = SubtitleController(
      provider: SubtitleProvider.fromNetwork(
    url,
    type: SubtitleType.vtt,
  ));

  await controller.initial();
  printResult(controller.subtitles);
}

void printResult(List<Subtitle> subtitles) {
  subtitles.sort((s1, s2) => s1.compareTo(s2));
  for (var result in subtitles) {
    print(
        '(${result.index}) Start: ${result.start}, end: ${result.end} [${result.data}]');
  }
}