subtitlesFromVideo static method

Future<List<SubtitleItem>> subtitlesFromVideo(
  1. {required File file,
  2. required int embeddedTrackCount,
  3. required dynamic onItemComplete(
    1. SubtitleItem
    )}
)

Gets a list of subtitles from a video file.

Implementation

static Future<List<SubtitleItem>> subtitlesFromVideo({
  required File file,
  required int embeddedTrackCount,
  required Function(SubtitleItem) onItemComplete,
}) async {
  List<File> outputFiles = [];
  List<SubtitleItem> items = [];

  Directory appDocDir = await getApplicationDocumentsDirectory();
  Directory subsDir = Directory('${appDocDir.path}/subtitles');
  if (!subsDir.existsSync()) {
    subsDir.createSync(recursive: true);
  }

  String inputPath = file.path;

  for (int i = 0; i < embeddedTrackCount - 1; i++) {
    String outputPath = '${subsDir.path}/extractSrt$i.srt';
    String command =
        '-loglevel quiet -i "$inputPath" -map 0:s:$i "$outputPath"';

    File outputFile = File(outputPath);

    if (outputFile.existsSync()) {
      outputFile.deleteSync();
    }

    final FlutterFFmpeg _flutterFFmpeg = FlutterFFmpeg();
    final FlutterFFmpegConfig _flutterFFmpegConfig = FlutterFFmpegConfig();

    await _flutterFFmpeg.execute(command);
    String output = await _flutterFFmpegConfig.getLastCommandOutput();
    if (output.contains("Stream map '0:s:$i' matches no streams.")) {
      break;
    }

    await Future.delayed(const Duration(seconds: 1));

    SubtitleItem item = await subtitlesFromFile(
      file: outputFile,
      type: SubtitleItemType.embeddedSubtitle,
      index: i,
    );

    onItemComplete(item);
    outputFiles.add(outputFile);
    items.add(item);
  }

  return items;
}