public static VideoInfo TryReadVideoInfoViaMp4(string fileName) { var info = new VideoInfo { Success = false }; try { var mp4Parser = new MP4Parser(fileName); if (mp4Parser.Moov != null && mp4Parser.VideoResolution.X > 0) { info.Width = mp4Parser.VideoResolution.X; info.Height = mp4Parser.VideoResolution.Y; info.TotalMilliseconds = mp4Parser.Duration.TotalSeconds * 1000; info.TotalSeconds = info.TotalMilliseconds / 1000.0; info.TotalFrames = mp4Parser.FrameRate * info.TotalSeconds; info.VideoCodec = "MP4"; info.FramesPerSecond = mp4Parser.FrameRate; info.Success = true; } } catch { // ignored } return(info); }
private void buttonOpenVideo_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog(this) == DialogResult.OK) { VideoFileName = openFileDialog1.FileName; labelVideoFileName.Text = VideoFileName; DateTime start; double durationInSeconds; string ext = Path.GetExtension(VideoFileName).ToLower(); if (ext == ".mp4" || ext == ".m4v" || ext == ".3gp") { MP4Parser mp4Parser = new MP4Parser(VideoFileName); start = mp4Parser.CreationDate; durationInSeconds = mp4Parser.Duration.TotalSeconds; } else { var fi = new FileInfo(VideoFileName); start = fi.CreationTime; VideoInfo vi = UiUtil.GetVideoInfo(VideoFileName); durationInSeconds = vi.TotalMilliseconds / TimeCode.BaseUnit; if (durationInSeconds < 1) { MessageBox.Show("Unable to get duration"); durationInSeconds = 60 * 60; } } dateTimePicker1.Value = start; timeUpDownStartTime.TimeCode = new TimeCode(start.Hour, start.Minute, start.Second, start.Millisecond); timeUpDownDuration.TimeCode = TimeCode.FromSeconds(durationInSeconds); } }
public void Mp4Test1() { string fileName = Path.Combine(Directory.GetCurrentDirectory(), "sample_MP4_SRT.mp4"); var parser = new MP4Parser(fileName); var tracks = parser.GetSubtitleTracks(); var paragraphs = tracks[0].Mdia.Minf.Stbl.GetParagraphs(); //1 //00:00:01,000-- > 00:00:03,000 //Line 1 //2 //00:00:03,024-- > 00:00:05,024 //Line 2 Assert.IsTrue(tracks.Count == 1); Assert.IsTrue(paragraphs.Count == 2); Assert.IsTrue(Math.Abs(paragraphs[0].StartTime.TotalMilliseconds - 1000) < 0.01); Assert.IsTrue(Math.Abs(paragraphs[0].EndTime.TotalMilliseconds - 3000) < 0.01); Assert.IsTrue(paragraphs[0].Text == "Line 1"); Assert.IsTrue(Math.Abs(paragraphs[1].StartTime.TotalMilliseconds - 3024) < 0.01); Assert.IsTrue(Math.Abs(paragraphs[1].EndTime.TotalMilliseconds - 5024) < 0.01); Assert.IsTrue(paragraphs[1].Text == "Line 2"); }
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName) { _errorCount = 0; subtitle.Paragraphs.Clear(); var mp4Parser = new MP4Parser(fileName); var dfxpStrings = mp4Parser.GetMdatsAsStrings(); foreach (var xmlAsString in dfxpStrings) { try { var sub = new Subtitle(); var mdatLines = xmlAsString.SplitToLines(); sub.ReloadLoadSubtitle(mdatLines, null, new TimedText()); if (sub.Paragraphs.Count == 0) continue; // merge lines with same time codes sub = Forms.MergeLinesWithSameTimeCodes.Merge(sub, new List<int>(), out _, true, false, 1000, "en", new List<int>(), new Dictionary<int, bool>(), new Subtitle()); // adjust to last exisiting sub var lastSub = subtitle.GetParagraphOrDefault(subtitle.Paragraphs.Count - 1); if (lastSub != null) sub.AddTimeToAllParagraphs(lastSub.EndTime.TimeSpan); subtitle.Paragraphs.AddRange(sub.Paragraphs); } catch { _errorCount++; } } }
public override void LoadSubtitle(Subtitle subtitle, List <string> lines, string fileName) { _errorCount = 0; subtitle.Paragraphs.Clear(); var mp4Parser = new MP4Parser(fileName); var dfxpStrings = mp4Parser.GetMdatsAsStrings(); SubtitleFormat format = new TimedText10(); foreach (var xmlAsString in dfxpStrings) { try { if (xmlAsString.Length < 80) { continue; } if (xmlAsString.IndexOf('\0') >= 0) { _errorCount++; continue; } var sub = new Subtitle(); var mdatLines = xmlAsString.SplitToLines(25_000); format = sub.ReloadLoadSubtitle(mdatLines, null, format); if (sub.Paragraphs.Count == 0) { continue; } // merge lines with same time codes sub = Forms.MergeLinesWithSameTimeCodes.Merge(sub, new List <int>(), out _, true, false, 1000, "en", new List <int>(), new Dictionary <int, bool>(), new Subtitle()); // adjust to last exisiting sub var lastSub = subtitle.GetParagraphOrDefault(subtitle.Paragraphs.Count - 1); if (lastSub != null && sub.Paragraphs.Count > 0 && lastSub.StartTime.TotalMilliseconds > sub.Paragraphs[0].StartTime.TotalMilliseconds) { sub.AddTimeToAllParagraphs(lastSub.EndTime.TimeSpan); } subtitle.Paragraphs.AddRange(sub.Paragraphs); } catch { _errorCount++; } } var merged = MergeLinesSameTextUtils.MergeLinesWithSameTextInSubtitle(subtitle, false, 250); if (merged.Paragraphs.Count < subtitle.Paragraphs.Count) { subtitle.Paragraphs.Clear(); subtitle.Paragraphs.AddRange(merged.Paragraphs); } subtitle.Renumber(); }
public void Mp4Test1() { string fileName = Path.Combine(Directory.GetCurrentDirectory(), "sample_MP4_SRT.mp4"); var parser = new MP4Parser(fileName); var tracks = parser.GetSubtitleTracks(); Assert.IsTrue(tracks.Count == 1); Assert.IsTrue(tracks[0].Mdia.Minf.Stbl.EndTimeCodes.Count == 2); }
private void AddWaveform_Shown(object sender, EventArgs e) { Refresh(); var audioTrackNames = new List <string>(); var mkvAudioTrackNumbers = new Dictionary <int, int>(); int numberOfAudioTracks = 0; if (labelVideoFileName.Text.Length > 1 && File.Exists(labelVideoFileName.Text)) { if (labelVideoFileName.Text.EndsWith(".mkv", StringComparison.OrdinalIgnoreCase)) { // Choose for number of audio tracks in matroska files MatroskaFile matroska = null; try { matroska = new MatroskaFile(labelVideoFileName.Text); if (matroska.IsValid) { foreach (var track in matroska.GetTracks()) { if (track.IsAudio) { numberOfAudioTracks++; if (track.CodecId != null && track.Language != null) { audioTrackNames.Add("#" + track.TrackNumber + ": " + track.CodecId.Replace("\0", string.Empty) + " - " + track.Language.Replace("\0", string.Empty)); } else { audioTrackNames.Add("#" + track.TrackNumber); } mkvAudioTrackNumbers.Add(mkvAudioTrackNumbers.Count, track.TrackNumber); } } } } finally { matroska?.Dispose(); } } else if (labelVideoFileName.Text.EndsWith(".mp4", StringComparison.OrdinalIgnoreCase) || labelVideoFileName.Text.EndsWith(".m4v", StringComparison.OrdinalIgnoreCase)) { // Choose for number of audio tracks in mp4 files try { var mp4 = new MP4Parser(labelVideoFileName.Text); var tracks = mp4.GetAudioTracks(); int i = 0; foreach (var track in tracks) { i++; if (track.Name != null && track.Mdia != null && track.Mdia.Mdhd != null && track.Mdia.Mdhd.LanguageString != null) { audioTrackNames.Add(i + ": " + track.Name + " - " + track.Mdia.Mdhd.LanguageString); } else if (track.Name != null) { audioTrackNames.Add(i + ": " + track.Name); } else { audioTrackNames.Add(i.ToString(CultureInfo.InvariantCulture)); } } numberOfAudioTracks = tracks.Count; } catch { // ignored } } // Choose audio track if (numberOfAudioTracks > 1) { using (var form = new ChooseAudioTrack(audioTrackNames, _audioTrackNumber)) { if (form.ShowDialog(this) == DialogResult.OK) { _audioTrackNumber = form.SelectedTrack; } else { DialogResult = DialogResult.Cancel; return; } } } // check for delay in matroska files if (labelVideoFileName.Text.EndsWith(".mkv", StringComparison.OrdinalIgnoreCase)) { MatroskaFile matroska = null; try { matroska = new MatroskaFile(labelVideoFileName.Text); if (matroska.IsValid) { _delayInMilliseconds = (int)matroska.GetTrackStartTime(mkvAudioTrackNumbers[_audioTrackNumber]); } } catch { _delayInMilliseconds = 0; } finally { matroska?.Dispose(); } } buttonRipWave_Click(null, null); } else if (_wavFileName != null) { FixWaveOnly(); } }
private void AddWaveform_Shown(object sender, EventArgs e) { Refresh(); _numberOfAudioTracks = 0; var audioTrackNames = new List <string>(); var mkvAudioTrackNumbers = new Dictionary <int, int>(); if (labelVideoFileName.Text.Length > 1 && File.Exists(labelVideoFileName.Text)) { if (labelVideoFileName.Text.EndsWith(".mkv", StringComparison.OrdinalIgnoreCase)) { // Choose for number of audio tracks in matroska files MatroskaFile matroska = null; try { matroska = new MatroskaFile(labelVideoFileName.Text); if (matroska.IsValid) { foreach (var track in matroska.GetTracks()) { if (track.IsAudio) { _numberOfAudioTracks++; if (track.CodecId != null && track.Language != null) { audioTrackNames.Add("#" + track.TrackNumber + ": " + track.CodecId.Replace("\0", string.Empty) + " - " + track.Language.Replace("\0", string.Empty)); } else { audioTrackNames.Add("#" + track.TrackNumber); } mkvAudioTrackNumbers.Add(mkvAudioTrackNumbers.Count, track.TrackNumber); } } } } finally { matroska?.Dispose(); } } else if (labelVideoFileName.Text.EndsWith(".mp4", StringComparison.OrdinalIgnoreCase) || labelVideoFileName.Text.EndsWith(".m4v", StringComparison.OrdinalIgnoreCase)) { // Choose for number of audio tracks in mp4 files try { var mp4 = new MP4Parser(labelVideoFileName.Text); var tracks = mp4.GetAudioTracks(); int i = 0; foreach (var track in tracks) { i++; if (track.Name != null && track.Mdia != null && track.Mdia.Mdhd != null && track.Mdia.Mdhd.LanguageString != null) { audioTrackNames.Add(i + ": " + track.Name + " - " + track.Mdia.Mdhd.LanguageString); } else if (track.Name != null) { audioTrackNames.Add(i + ": " + track.Name); } else { audioTrackNames.Add(i.ToString(CultureInfo.InvariantCulture)); } } _numberOfAudioTracks = tracks.Count; } catch { // ignored } } // Choose audio track if (_numberOfAudioTracks > 1) { using (var form = new ChooseAudioTrack(audioTrackNames, AudioTrackNumber)) { if (form.ShowDialog(this) == DialogResult.OK) { if (AudioTrackNumber != form.SelectedTrack) { AudioTrackNumber = form.SelectedTrack; var peakWaveFileName = WavePeakGenerator.GetPeakWaveFileName(labelVideoFileName.Text, form.SelectedTrack); var spectrogramFolder = WavePeakGenerator.SpectrogramDrawer.GetSpectrogramFolder(labelVideoFileName.Text, form.SelectedTrack); if (File.Exists(peakWaveFileName)) { DialogResult = DialogResult.Cancel; return; } _peakWaveFileName = peakWaveFileName; _spectrogramDirectory = spectrogramFolder; } } else { DialogResult = DialogResult.Cancel; return; } } } // check for delay in matroska files if (labelVideoFileName.Text.EndsWith(".mkv", StringComparison.OrdinalIgnoreCase)) { MatroskaFile matroska = null; try { matroska = new MatroskaFile(labelVideoFileName.Text); if (matroska.IsValid) { _delayInMilliseconds = (int)matroska.GetAudioTrackDelayMilliseconds(mkvAudioTrackNumbers[AudioTrackNumber]); } } catch (Exception exception) { SeLogger.Error(exception, $"Error getting delay from mkv: {labelVideoFileName.Text}"); _delayInMilliseconds = 0; } finally { matroska?.Dispose(); } } buttonRipWave_Click(null, null); } else if (_wavFileName != null) { FixWaveOnly(); } }
private static VideoInfo TryReadVideoInfoViaMp4(string fileName) { var info = new VideoInfo { Success = false }; try { var mp4Parser = new MP4Parser(fileName); if (mp4Parser.Moov != null && mp4Parser.VideoResolution.X > 0) { info.Width = mp4Parser.VideoResolution.X; info.Height = mp4Parser.VideoResolution.Y; info.TotalMilliseconds = mp4Parser.Duration.TotalSeconds; info.VideoCodec = "MP4"; info.FramesPerSecond = mp4Parser.FrameRate; info.Success = true; } } catch { } return info; }
private void AddWareForm_Shown(object sender, EventArgs e) { Refresh(); var audioTrackNames = new List<string>(); var mkvAudioTrackNumbers = new Dictionary<int, int>(); int numberOfAudioTracks = 0; if (labelVideoFileName.Text.Length > 1 && File.Exists(labelVideoFileName.Text)) { if (labelVideoFileName.Text.EndsWith(".mkv", StringComparison.OrdinalIgnoreCase)) { // Choose for number of audio tracks in matroska files MatroskaFile matroska = null; try { matroska = new MatroskaFile(labelVideoFileName.Text); if (matroska.IsValid) { foreach (var track in matroska.GetTracks()) { if (track.IsAudio) { numberOfAudioTracks++; if (track.CodecId != null && track.Language != null) audioTrackNames.Add("#" + track.TrackNumber + ": " + track.CodecId.Replace("\0", string.Empty) + " - " + track.Language.Replace("\0", string.Empty)); else audioTrackNames.Add("#" + track.TrackNumber); mkvAudioTrackNumbers.Add(mkvAudioTrackNumbers.Count, track.TrackNumber); } } } } finally { if (matroska != null) { matroska.Dispose(); } } } else if (labelVideoFileName.Text.EndsWith(".mp4", StringComparison.OrdinalIgnoreCase) || labelVideoFileName.Text.EndsWith(".m4v", StringComparison.OrdinalIgnoreCase)) { // Choose for number of audio tracks in mp4 files try { var mp4 = new MP4Parser(labelVideoFileName.Text); var tracks = mp4.GetAudioTracks(); int i = 0; foreach (var track in tracks) { i++; if (track.Name != null && track.Mdia != null && track.Mdia.Mdhd != null && track.Mdia.Mdhd.LanguageString != null) audioTrackNames.Add(i + ": " + track.Name + " - " + track.Mdia.Mdhd.LanguageString); else if (track.Name != null) audioTrackNames.Add(i + ": " + track.Name); else audioTrackNames.Add(i.ToString(CultureInfo.InvariantCulture)); } numberOfAudioTracks = tracks.Count; } catch { } } if (Configuration.Settings.General.UseFFmpegForWaveExtraction) { // don't know how to extract audio number x via FFmpeg... numberOfAudioTracks = 1; _audioTrackNumber = 0; } // Choose audio track if (numberOfAudioTracks > 1) { using (var form = new ChooseAudioTrack(audioTrackNames, _audioTrackNumber)) { if (form.ShowDialog(this) == DialogResult.OK) { _audioTrackNumber = form.SelectedTrack; } else { DialogResult = DialogResult.Cancel; return; } } } // check for delay in matroska files if (labelVideoFileName.Text.EndsWith(".mkv", StringComparison.OrdinalIgnoreCase)) { MatroskaFile matroska = null; try { matroska = new MatroskaFile(labelVideoFileName.Text); if (matroska.IsValid) { _delayInMilliseconds = (int)matroska.GetTrackStartTime(mkvAudioTrackNumbers[_audioTrackNumber]); } } catch { _delayInMilliseconds = 0; } finally { if (matroska != null) { matroska.Dispose(); } } } buttonRipWave_Click(null, null); } else if (_wavFileName != null) { FixWaveOnly(); } }