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(); Assert.IsTrue(tracks.Count == 1); Assert.IsTrue(tracks[0].Mdia.Minf.Stbl.EndTimeCodes.Count == 2); }
/* private readonly static String[] colors = { "{\\c&HC0C0C0&}", // black /gray "{\\c&H4040FF&}", // red "{\\c&H00FF00&}", // green "{\\c&H00FFFF&}", // yellow "{\\c&HFF409B&}", // blue //DM15032004 081.6 int18 changed "{\\c&HFF00FF&}", // magenta "{\\c&HFFFF00&}", // cyan "{\\c&HFFFFFF&}", // white }; public static byte ByteReverse(byte n) { n = (byte)(((n >> 1) & 0x55) | ((n << 1) & 0xaa)); n = (byte)(((n >> 2) & 0x33) | ((n << 2) & 0xcc)); n = (byte)(((n >> 4) & 0x0f) | ((n << 4) & 0xf0)); return n; } private static string GetTeletext(byte[] _buffer, int offset) { string text = string.Empty; bool ascii = false; const int color = 0; bool toggle = false; for (int c = offset, i = 0; c < _buffer.Length; c++, i++) { //var char_value = _buffer[c]; var char_value = 0x7F & ByteReverse(_buffer[c]); if (char_value >> 3 == 0) //0x0..7 { ascii = true; text += ((color == 1) ? colors[char_value] : string.Empty); // + (char)active_set[32]; } else if (char_value >> 4 == 0) //0x8..F { text += " "; //(char)active_set[32]; } else if (char_value >> 7 == 1) //0x80..FF { text += " "; //(char)active_set[32]; } else if (char_value < 27) //0x10..1A { ascii = false; text += " "; //(char)active_set[32]; } else if (char_value < 32) //0x1B..1F { if (char_value == 0x1B) //ESC { if (toggle) { // active_set = CharSet.getActive_G0_Set(primary_set_mapping, primary_national_set_mapping, row); // active_national_set = CharSet.getActiveNationalSubset(primary_set_mapping, primary_national_set_mapping, row); } else { //active_set = CharSet.getActive_G0_Set(secondary_set_mapping, secondary_national_set_mapping, row); //active_national_set = CharSet.getActiveNationalSubset(secondary_set_mapping, secondary_national_set_mapping, row); } toggle = !toggle; } text += " "; //(char)active_set[32]; continue; } else if (char_value == 0x7F) //0x7F { text += " "; // (char)active_set[32]; continue; } if (!ascii) { text += " "; // (char)active_set[32]; continue; } //if (active_national_set != null) //{ // // all chars 0x20..7F // switch (char_value) // special national characters // { // case 0x23: // text += (char)active_national_set[0]; // continue loopi; // case 0x24: // text += (char)active_national_set[1]; // continue loopi; // case 0x40: // text += (char)active_national_set[2]; // continue loopi; // case 0x5b: // text += (char)active_national_set[3]; // continue loopi; // case 0x5c: // text += (char)active_national_set[4]; // continue loopi; // case 0x5d: // text += (char)active_national_set[5]; // continue loopi; // case 0x5e: // text += (char)active_national_set[6]; // continue loopi; // case 0x5f: // text += (char)active_national_set[7]; // continue loopi; // case 0x60: // text += (char)active_national_set[8]; // continue loopi; // case 0x7b: // text += (char)active_national_set[9]; // continue loopi; // case 0x7c: // text += (char)active_national_set[10]; // continue loopi; // case 0x7d: // text += (char)active_national_set[11]; // continue loopi; // case 0x7e: // text += (char)active_national_set[12]; // continue loopi; // } //} text += Encoding.Default.GetString(new byte[] { (byte)char_value }); //(char)active_set[char_value]; //continue loopi; } if (color == 1) return colors[7] + text.Trim(); else return text; } */ #endregion Teletext private bool ImportSubtitleFromMp4(string fileName) { var mp4Parser = new MP4Parser(fileName); var mp4SubtitleTracks = mp4Parser.GetSubtitleTracks(); if (mp4SubtitleTracks.Count == 0) { MessageBox.Show(_language.NoSubtitlesFound); return false; } else if (mp4SubtitleTracks.Count == 1) { LoadMp4Subtitle(fileName, mp4SubtitleTracks[0]); return true; } else { using (var subtitleChooser = new MatroskaSubtitleChooser()) { subtitleChooser.Initialize(mp4SubtitleTracks); if (subtitleChooser.ShowDialog(this) == DialogResult.OK) { LoadMp4Subtitle(fileName, mp4SubtitleTracks[subtitleChooser.SelectedIndex]); return true; } } return false; } }
private void pointSyncViaOtherSubtitleToolStripMenuItem_Click(object sender, EventArgs e) { using (var pointSync = new SyncPointsSync()) { openFileDialog1.Title = _language.OpenOtherSubtitle; openFileDialog1.FileName = string.Empty; openFileDialog1.Filter = Utilities.GetOpenDialogFilter(); if (openFileDialog1.ShowDialog() == DialogResult.OK && File.Exists(openFileDialog1.FileName)) { var sub = new Subtitle(); var file = new FileInfo(openFileDialog1.FileName); var fileName = file.FullName; var extension = file.Extension.ToLowerInvariant(); // TODO: Check for mkv etc if (extension == ".sub") { if (IsVobSubFile(fileName, false)) { MessageBox.Show(_language.NoSupportHereVobSub); return; } } if (extension == ".sup") { if (FileUtil.IsBluRaySup(fileName)) { MessageBox.Show(_language.NoSupportHereBluRaySup); return; } else if (FileUtil.IsSpDvdSup(fileName)) { MessageBox.Show(_language.NoSupportHereDvdSup); return; } } if (extension == ".mkv" || extension == ".mks") { using (var matroska = new MatroskaFile(fileName)) { if (matroska.IsValid) { var subtitleList = matroska.GetTracks(true); if (subtitleList.Count > 1) { using (var subtitleChooser = new MatroskaSubtitleChooser()) { subtitleChooser.Initialize(subtitleList); if (_loading) { subtitleChooser.Icon = (Icon)this.Icon.Clone(); subtitleChooser.ShowInTaskbar = true; subtitleChooser.ShowIcon = true; } if (subtitleChooser.ShowDialog(this) == DialogResult.OK) { sub = LoadMatroskaSubtitleForSync(subtitleList[subtitleChooser.SelectedIndex], matroska); } } } else if (subtitleList.Count > 0) { sub = LoadMatroskaSubtitleForSync(subtitleList[0], matroska); } else { MessageBox.Show(_language.NoSubtitlesFound); return; } } } } if (extension == ".divx" || extension == ".avi") { MessageBox.Show(_language.NoSupportHereDivx); return; } if ((extension == ".mp4" || extension == ".m4v" || extension == ".3gp") && file.Length > 10000) { var mp4Parser = new MP4Parser(fileName); var mp4SubtitleTracks = mp4Parser.GetSubtitleTracks(); if (mp4SubtitleTracks.Count == 0) { MessageBox.Show(_language.NoSubtitlesFound); return; } else if (mp4SubtitleTracks.Count == 1) { sub = LoadMp4SubtitleForSync(mp4SubtitleTracks[0]); } else { using (var subtitleChooser = new MatroskaSubtitleChooser()) { subtitleChooser.Initialize(mp4SubtitleTracks); if (subtitleChooser.ShowDialog(this) == DialogResult.OK) { sub = LoadMp4SubtitleForSync(mp4SubtitleTracks[0]); } } } } if (file.Length > 1024 * 1024 * 10 && sub.Paragraphs.Count == 0) // max 10 mb { var text = string.Format(_language.FileXIsLargerThan10MB + Environment.NewLine + Environment.NewLine + _language.ContinueAnyway, fileName); if (MessageBox.Show(this, text, Title, MessageBoxButtons.YesNoCancel) != DialogResult.Yes) return; } sub.Renumber(); if (sub.Paragraphs.Count == 0) { Encoding enc; SubtitleFormat f = sub.LoadSubtitle(fileName, out enc, null); if (f == null) { ShowUnknownSubtitle(); return; } } pointSync.Initialize(_subtitle, _fileName, _videoFileName, _videoAudioTrackNumber, fileName, sub); mediaPlayer.Pause(); if (pointSync.ShowDialog(this) == DialogResult.OK) { _subtitleListViewIndex = -1; MakeHistoryForUndo(_language.BeforePointSynchronization); _subtitle.Paragraphs.Clear(); foreach (var p in pointSync.FixedSubtitle.Paragraphs) _subtitle.Paragraphs.Add(p); _subtitle.CalculateFrameNumbersFromTimeCodesNoCheck(CurrentFrameRate); ShowStatus(_language.PointSynchronizationDone); ShowSource(); SubtitleListview1.Fill(_subtitle, _subtitleAlternate); } _videoFileName = pointSync.VideoFileName; } } }
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; info.VideoCodec = "MP4"; info.FramesPerSecond = mp4Parser.FrameRate; info.Success = true; } } catch { } return info; }
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 { 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 { } } // 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(); } }
private void DoSubtitleListview1Drop(object sender, EventArgs e) { _dragAndDropTimer.Stop(); if (ContinueNewOrExit()) { string fileName = _dragAndDropFiles[0]; var file = new FileInfo(fileName); // Do not allow directory drop if (FileUtil.IsDirectory(fileName)) { MessageBox.Show(_language.ErrorDirectoryDropNotAllowed, file.Name, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } var dirName = Path.GetDirectoryName(fileName); saveFileDialog1.InitialDirectory = dirName; openFileDialog1.InitialDirectory = dirName; var ext = file.Extension.ToLowerInvariant(); if (ext == ".mkv" || ext == ".mks") { using (var matroska = new MatroskaFile(fileName)) { if (matroska.IsValid) { var subtitleList = matroska.GetTracks(true); if (subtitleList.Count == 0) { MessageBox.Show(_language.NoSubtitlesFound); } else if (subtitleList.Count > 1) { using (var subtitleChooser = new MatroskaSubtitleChooser()) { subtitleChooser.Initialize(subtitleList); if (subtitleChooser.ShowDialog(this) == DialogResult.OK) { if (LoadMatroskaSubtitle(subtitleList[subtitleChooser.SelectedIndex], matroska, false) && ext.Equals(".mkv", StringComparison.Ordinal)) { OpenVideo(fileName); } } } } else { if (LoadMatroskaSubtitle(subtitleList[0], matroska, false) && ext.Equals(".mkv", StringComparison.Ordinal)) { OpenVideo(fileName); } } return; } } } else if (ext == ".mp4" || ext == ".m4v" || ext == ".3gp") { var mp4Parser = new MP4Parser(fileName); var mp4SubtitleTracks = mp4Parser.GetSubtitleTracks(); if (mp4SubtitleTracks.Count > 0) { ImportSubtitleFromMp4(fileName); return; } } else if (ext == ".vob" || ext == ".ifo") { ImportDvdSubtitle(fileName); return; } else if (ext == ".idx") { var subFileName = fileName.Substring(0, fileName.Length - 3) + "sub"; if (File.Exists(subFileName) && FileUtil.IsVobSub(subFileName)) { ImportAndOcrVobSubSubtitleNew(subFileName, _loading); return; } } if (file.Length < 1024 * 1024 * 2) // max 2 mb { OpenSubtitle(fileName, null); } else if (file.Length < 150000000 && ext == ".sub" && IsVobSubFile(fileName, true)) // max 150 mb { OpenSubtitle(fileName, null); } else if (file.Length < 250000000 && ext == ".sup" && FileUtil.IsBluRaySup(fileName)) // max 250 mb { OpenSubtitle(fileName, null); } else if ((ext == ".ts" || ext == ".rec" || ext == ".mpg" || ext == ".mpeg") && FileUtil.IsTransportStream(fileName)) { OpenSubtitle(fileName, null); } else if (ext == ".m2ts" && FileUtil.IsM2TransportStream(fileName)) { OpenSubtitle(fileName, null); } else { MessageBox.Show(string.Format(_language.DropFileXNotAccepted, fileName)); } } }