public async static Task <IEnumerable <SegmentModel> > LoadSegments(APIConnection api, FileIdentifier identifier) { var parser = new VTTFormat(); using (var ms = new MemoryStream()) { IEnumerable <SegmentModel> segments = null; await api.File.DownloadAsync(identifier, async (stream, cancel) => { var items = await parser.ParseStreamAsync(stream); segments = items.Select(i => { var text = i.Lines != null ? string.Join('\n', i.Lines) : string.Empty; return(new SegmentModel { StartTime = (int)i.StartTime.TotalMilliseconds, EndTime = (int)i.EndTime.TotalMilliseconds, Text = text, TextOriginal = text }); }); }); return(segments); } }
public async Task DeleteInsertAsync(FileIdentifier identifier, TranscriptSet transcriptSetToInsert = null) { var rootFile = await Connection.File.GetAsync(identifier); var oldTranscriptJsonIdentifier = FindViewByName(rootFile, FILENAME_JSON); var oldTranscriptVttIdentifier = FindViewByName(rootFile, FILENAME_VTT); FileModel newTranscriptJSONFile = null; FileModel newTranscriptVTTFile = null; if (transcriptSetToInsert != null) { newTranscriptJSONFile = await CreateHiddenChildFileAsync( rootFile.Identifier, FILENAME_JSON, JsonConvert.SerializeObject(transcriptSetToInsert.Segments), "application/json" ); var vttFormat = new VTTFormat(); int segmentIndexNumber = 0; var vttStringContent = vttFormat.CreateVTT(transcriptSetToInsert.Segments .Select(s => new SubtitleSegmentModel { StartTime = TimeSpan.FromMilliseconds(s.StartTime), EndTime = TimeSpan.FromMilliseconds(s.EndTime), Lines = s.Text?.Split('\n').ToList(), SegmentIndex = ++segmentIndexNumber }).ToList() ); newTranscriptVTTFile = await CreateHiddenChildFileAsync( rootFile.Identifier, FILENAME_VTT, vttStringContent, "text/vtt" ); } await Connection.ConcurrencyRetryBlock(async() => { // get the current file rootFile = await Connection.File.GetAsync(identifier); ReplaceViewByName(rootFile, FILENAME_JSON, "application/json", newTranscriptJSONFile.Identifier); ReplaceViewByName(rootFile, FILENAME_VTT, "text/vtt", newTranscriptVTTFile.Identifier); await Connection.File.PutAsync(rootFile); }); if (oldTranscriptJsonIdentifier != null) { await Connection.File.DeleteAsync(oldTranscriptJsonIdentifier); } if (oldTranscriptVttIdentifier != null) { await Connection.File.DeleteAsync(oldTranscriptVttIdentifier); } }