private void ParseMp4(FileStream fs) { int count = 0; Position = 0; fs.Seek(0, SeekOrigin.Begin); bool moreBytes = true; while (moreBytes) { moreBytes = InitializeSizeAndName(fs); if (Size < 8) { return; } if (Name == "moov") { Moov = new Moov(fs, Position); } count++; if (count > 100) { break; } if (Position > (ulong)fs.Length) { break; } fs.Seek((long)Position, SeekOrigin.Begin); } fs.Close(); }
private void ParseCmaf(Stream fs) { Subtitle = new Subtitle(); int count = 0; Position = 0; fs.Seek(0, SeekOrigin.Begin); bool moreBytes = true; var samples = new List <TimeSegment>(); var payloads = new List <string>(); while (moreBytes) { moreBytes = InitializeSizeAndName(fs); if (Size < 8) { return; } if (Name == "moov" && Moov == null) { Moov = new Moov(fs, Position); // only scan first "moov" element } else if (Name == "mdat") { var mdat = new Mdat(fs, Position); if (mdat.Payloads.Count > 0) { payloads.AddRange(mdat.Payloads); } } else if (Name == "moof") { Moof = new Moof(fs, Position); if (Moof.Traf?.Trun?.Samples.Count > 0) { samples.AddRange(Moof.Traf.Trun.Samples); } } count++; if (count > 10000) { break; } if (Position > (ulong)fs.Length) { break; } fs.Seek((long)Position, SeekOrigin.Begin); } fs.Close(); ulong timePeriodStart = 0; // ??? ulong timeScale = 0; if (Moov?.Tracks[0].Mdia.Mdhd.TimeScale > 0) { timeScale = Moov.Tracks[0].Mdia.Mdhd.TimeScale; } if (Moov?.Mvhd?.TimeScale > 0) { timeScale = Moov.Mvhd.TimeScale; } int max = Math.Min(samples.Count, payloads.Count); for (var i = 0; i < max; i++) { var presentation = samples[i]; var payload = payloads[i]; if (presentation.Duration.HasValue) { var startTime = presentation.TimeOffset.HasValue ? presentation.BaseMediaDecodeTime + presentation.TimeOffset.Value : presentation.BaseMediaDecodeTime; var currentTime = startTime + presentation.Duration.Value; // The payload can be null as that would mean that it was a VTTE and // was only inserted to keep the presentation times in sync with the // payloads. if (payload != null) { Subtitle.Paragraphs.Add(new Paragraph(payload, timePeriodStart + startTime / timeScale, timePeriodStart + currentTime / timeScale)); } } } Subtitle.Renumber(); }
private void ParseMp4(Stream fs) { int count = 0; Position = 0; fs.Seek(0, SeekOrigin.Begin); bool moreBytes = true; var timeTotalMs = 0d; while (moreBytes) { moreBytes = InitializeSizeAndName(fs); if (Size < 8) { return; } if (Name == "moov" && Moov == null) { Moov = new Moov(fs, Position); // only scan first "moov" element } else if (Name == "moof") { Moof = new Moof(fs, Position); } else if (Name == "mdat" && Moof != null && Moof?.Traf?.Trun?.Samples?.Count > 0) { var mdat = new Mdat(fs, Position); if (mdat.Payloads.Count > 0) { if (Moof.Traf?.Trun?.Samples.Count > 0 && Moof?.Traf?.Trun?.Samples.Count >= mdat.Payloads.Count) { if (VttcSubtitle == null) { VttcSubtitle = new Subtitle(); } var timeScale = (double)(Moov?.Mvhd?.TimeScale ?? 1000.0); var sampleIdx = 0; foreach (var payload in mdat.Payloads) { var presentation = Moof.Traf.Trun.Samples[sampleIdx]; if (presentation.Duration.HasValue) { var before = timeTotalMs; timeTotalMs += presentation.Duration.Value / timeScale * 1000.0; sampleIdx++; if (payload != null) { VttcSubtitle.Paragraphs.Add(new Paragraph(payload, before, timeTotalMs)); } } } } Moof = null; } } count++; if (count > 1000) { break; } if (Position > (ulong)fs.Length) { break; } fs.Seek((long)Position, SeekOrigin.Begin); } fs.Close(); if (VttcSubtitle != null) { var merged = MergeLinesSameTextUtils.MergeLinesWithSameTextInSubtitle(VttcSubtitle, false, 250); VttcSubtitle = merged; } }
private void ParseMp4(FileStream fs, TreeView treeView) { var samples = new List <TimeSegment>(); var payloads = new List <string>(); int count = 0; Position = 0; fs.Seek(0, SeekOrigin.Begin); bool moreBytes = true; while (moreBytes) { moreBytes = InitializeSizeAndName(fs); if (Size < 8) { return; } if (Name == "moov") { var node = new TreeNode(Name) { Tag = "Element: " + Name + " - Movie" + Environment.NewLine + "Size: " + Size }; treeView?.Nodes.Add(node); Moov = new Moov(fs, Position, node); } else if (Name == "mdat") { var node = new TreeNode(Name) { Tag = "Element: " + Name + " - Media Data" + Environment.NewLine + "Size: " + Size + Environment.NewLine + "Position: " + StartPosition }; treeView?.Nodes.Add(node); var mdat = new Mdat(fs, Position, node); if (mdat.Payloads.Count > 0) { payloads.AddRange(mdat.Payloads); } } else if (Name == "moof") { var node = new TreeNode(Name) { Tag = "Element: " + Name + " - " + Environment.NewLine + "Size: " + Size + Environment.NewLine + "Position: " + StartPosition }; treeView?.Nodes.Add(node); Moof = new Moof(fs, Position, node); if (Moof.Traf?.Trun?.Samples.Count > 0) { samples.AddRange(Moof.Traf.Trun.Samples); } } else { treeView?.Nodes.Add(new TreeNode(Name) { Tag = "Element: " + Name + " - " + Environment.NewLine + "Size: " + Size + Environment.NewLine + "Position: " + StartPosition }); } count++; if (count > 1000) { break; } if (Position > (ulong)fs.Length) { break; } fs.Seek((long)Position, SeekOrigin.Begin); } fs.Close(); }
private void ParseMp4(FileStream fs) { int count = 0; Position = 0; fs.Seek(0, SeekOrigin.Begin); bool moreBytes = true; while (moreBytes) { moreBytes = InitializeSizeAndName(fs); if (Size < 8) { return; } if (Name == "moov" && Moov == null) { Moov = new Moov(fs, Position); // only scan first "moov" element } count++; if (count > 100) { break; } if (Position > (ulong)fs.Length) { break; } fs.Seek((long)Position, SeekOrigin.Begin); } fs.Close(); }