subtitlesFromFile static method

Future<SubtitleItem> subtitlesFromFile(
  1. {required File file,
  2. required SubtitleItemType type,
  3. String? source,
  4. String? metadata,
  5. int? index}
)

Fetches a subtitle from a subtitle file.

Implementation

static Future<SubtitleItem> subtitlesFromFile({
  required File file,
  required SubtitleItemType type,
  String? source,
  String? metadata,
  int? index,
}) async {
  String fileExtension = path.extension(file.path).toLowerCase();

  if (!file.existsSync()) {
    return SubtitleItem(
      controller: SubtitleController(
        provider: SubtitleProvider.fromFile(
          file,
        ),
      ),
      metadata: metadata,
      source: source,
      type: type,
      index: index,
    );
  }

  switch (fileExtension) {
    case '.srt':
      return SubtitleItem(
        controller: SubtitleController(
          provider: SubtitleProvider.fromString(
            data: file.readAsStringSync(),
            type: SubtitleType.srt,
          ),
        ),
        metadata: metadata,
        type: type,
        index: index,
      );
    case '.ass':
    case '.ssa':
      return SubtitleItem(
        controller: SubtitleController(
          provider: SubtitleProvider.fromString(
            data: await convertAssSubtitles(file.path),
            type: SubtitleType.srt,
          ),
        ),
        metadata: metadata,
        type: type,
        index: index,
      );
  }

  return SubtitleItem(
    controller: SubtitleController(
      provider: SubtitleProvider.fromFile(
        file,
      ),
    ),
    metadata: metadata,
    type: type,
    index: index,
  );
}