/// <summary> /// /// </summary> /// <param name="box"></param> public Mp4TreeNode(Mp4Box box) { this.Box = box; Text = Mp4Util.FormatFourChars(box.Type); ImageSource = GetImageSource(box.Type); if (box is Mp4ContainerBox) { foreach (Mp4Box child in ((Mp4ContainerBox)box).Children) { Items.Add(new Mp4TreeNode(child)); } } if (box is Mp4StsdBox) { foreach (Mp4Box child in ((Mp4StsdBox)box).Entries) { Items.Add(new Mp4TreeNode(child)); } } if (box is Mp4DrefBox) { foreach (Mp4Box child in ((Mp4DrefBox)box).Entries) { Items.Add(new Mp4TreeNode(child)); } } }
/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void View_NodeSelected(object sender, DataEventArgs <TreeViewItem> e) { if (e.Value != null && e.Value is Mp4TreeNode) { Mp4Box box = ((Mp4TreeNode)e.Value).Box; controller.OnBoxSelected(box); } else { controller.ShowFile(file); } }
public IBoxView GetView(Mp4Box box) { if (_ViewsMap.TryGetValue(box.GetType(), out Type viewType)) { var view = (IBoxView)Activator.CreateInstance(viewType); view.Box = box; return(view); } else { return(null); } }
public void OnBoxSelected(Mp4Box box) { RemoveViews(); if (box != null) { IBoxView view = _MainService.GetView(box); if (view != null) { IRegion mainRegion = _RegionManager.Regions[RegionNames.MainRegion]; mainRegion.Add(view); mainRegion.Activate(view); } } }
public static bool ParseFile(string Path, Options opt) { bool result = false; string log = "\r\n"; try { FileStream fs = new FileStream(Path, FileMode.Open, FileAccess.Read); if (fs != null) { long offset = 0; fs.Seek((long)offset, SeekOrigin.Begin); while (offset < fs.Length) { Mp4Box box = Mp4Box.ReadMp4Box(fs); if (box != null) { log += box.ToString() + "\tat offset: " + offset.ToString() + "\r\n"; if (box.GetBoxType() != "mdat\0") { log += Mp4Box.GetBoxChildrenString(0, box); } if (((opt.TraceLevel >= Options.LogLevel.Verbose) && (!string.IsNullOrEmpty(opt.TraceFile))) || (opt.ConsoleLevel >= Options.LogLevel.Verbose)) { log += Options.DumpHex(box.GetBoxBytes()); } offset += box.GetBoxLength(); opt.LogInformation(log); log = "\r\n"; } else { break; } } fs.Close(); result = true; } } catch (Exception ex) { opt.LogError("ERROR: Exception while parsing the file: " + ex.Message); } return(result); }
/// <summary> /// /// </summary> /// <param name="box"></param> /// <returns></returns> public IBoxView GetView(Mp4Box box) { Type type = null; if (!viewsCache.ContainsKey(box.GetType())) { Assembly assembly = Assembly.GetAssembly(this.GetType()); List <Type> types = new List <Type>(assembly.GetTypes()); type = types.Find(t => { object[] customAttributes = t.GetCustomAttributes(typeof(BoxViewTypeAttribute), false); return(typeof(IBoxView).IsAssignableFrom(t) && customAttributes.Length == 1 && ((BoxViewTypeAttribute)customAttributes[0]).BoxType == box.GetType()); }); if (type != null) { viewsCache.Add(box.GetType(), type); } else { type = null; } } else { type = viewsCache[box.GetType()]; } if (type != null) { IBoxView view = (IBoxView)type.GetConstructor(Type.EmptyTypes).Invoke(null); view.Box = box; return(view); } else { return(null); } }
public Mp4TreeNode(Mp4Box box) { Box = box; Text = Mp4Util.FormatFourChars(box.Type); ImageSource = GetImageSource(box.Type); IEnumerable <Mp4Box> children = box switch { Mp4ContainerBox containerBox => containerBox.Children, Mp4StsdBox stsdBox => stsdBox.Entries, Mp4DrefBox drefBox => drefBox.Entries, _ => Enumerable.Empty <Mp4Box>(), }; foreach (Mp4Box child in children) { Items.Add(new Mp4TreeNode(child)); } }
public static string ParseFile(string Path, Options opt, CallBack callback) { string result = "\r\n"; try { FileStream fs = new FileStream(Path, FileMode.Open, FileAccess.Read); if (fs != null) { long offset = 0; fs.Seek((long)offset, SeekOrigin.Begin); while (offset < fs.Length) { Mp4Box box = Mp4Box.ReadMp4Box(fs); if (box != null) { result += box.ToString() + "\tat offset: " + offset.ToString() + "\r\n"; if (box.GetBoxType() != "mdat\0") { result += Mp4Box.GetBoxChildrenString(0, box); } offset += box.GetBoxLength(); if (callback != null) { callback(opt, result); } result = "\r\n"; } else { break; } } fs.Close(); } } catch (Exception ex) { result += "ERROR: Exception while parsing the file: " + ex.Message; } return(result); }
static bool Parse(Options opt) { bool result = true; opt.Status = Options.TheadStatus.Running; opt.ThreadStartTime = DateTime.Now; opt.LogInformation("Parsing file: " + opt.InputUri); if (opt.TraceLevel >= Options.LogLevel.Verbose) { opt.LogVerbose(Mp4Box.ParseFileVerbose(opt.InputUri)); } else { opt.LogInformation(Mp4Box.ParseFile(opt.InputUri)); } opt.LogInformation("Parsing file: " + opt.InputUri + " done"); opt.Status = Options.TheadStatus.Stopped; return(result); }
static public void ConvertVideo(string mediaName, int mediaId) { var db = new ApplicationDbContext(); var dbRecord = db.MediaFiles.Find(mediaId); var baseDir = HostingEnvironment.MapPath("~/MediaData/Videos/" + mediaId); var mediaDir = Path.Combine(baseDir, mediaName); var mediaInfo = GetMediaInfo(mediaDir); //todo error when mediinfo doesnt have video if (!mediaInfo.Video.HasVideo) { //todo: implement this in the controller throw new Exception("Video file is not supported"); } VideoQuality videoQuality = ServerTools.VideoParams.ClassifyVideo(mediaInfo); MediaInfo videoParams = ServerTools.VideoParams.GetVideoParams(videoQuality); dbRecord.VideoQuality = videoQuality; var outputVidSd = Path.Combine(baseDir, "sd.mp4"); var outputVidHd = Path.Combine(baseDir, "hd.mp4"); var outputAudio = Path.Combine(baseDir, "audio.mp4"); var outputMobile = Path.Combine(baseDir, "mobile.mp4"); var outputMobileHd = Path.Combine(baseDir, "mobileHd.mp4"); var outputThumbnail = Path.Combine(baseDir, "thumbnail.jpg"); var outputPasslogFile = Path.Combine(baseDir, "passlog"); var segmentsDir = Path.Combine(baseDir, "segments"); if (!Directory.Exists(segmentsDir)) { Directory.CreateDirectory(segmentsDir); } var hcontext = HttpContext.Current; var task = Task.Factory.StartNew(() => { HttpContext.Current = hcontext; FFMPEG ffmpeg = new FFMPEG(); //What part of total progress is the current conversion Dictionary <string, double> ConversionPercentages = new Dictionary <string, double>(); if (videoQuality == VideoQuality.p360) { ConversionPercentages["sd"] = 0.9; ConversionPercentages["audio"] = 0.1; } else { ConversionPercentages["sd"] = 0.2; ConversionPercentages["hd"] = 0.7; ConversionPercentages["audio"] = 0.1; } // Convert to SD string command = String.Format("-i \"{0}\" -an -b:v {1}k -s {2} -vcodec libx264 -r 24 -g 48 -keyint_min 48 -sc_threshold 0 -pass 1 -passlogfile \"{3}\" \"{4}\"", mediaDir, ServerTools.VideoParams.p360.Video.Bitrate, ServerTools.VideoParams.p360.Video.Resolution, outputPasslogFile, outputVidSd); var result = ffmpeg.RunCommand(command, mediaId, ConversionPercentages, "sd", mediaInfo.Video.Duration); //Convert to HD if (videoQuality != VideoQuality.p360) { command = String.Format("-i \"{0}\" -an -b:v {1}k -s {2} -vcodec libx264 -r 24 -g 48 -keyint_min 48 -sc_threshold 0 -pass 1 -passlogfile \"{3}\" \"{4}\"", mediaDir, videoParams.Video.Bitrate, videoParams.Video.Resolution, outputPasslogFile, outputVidHd); result = ffmpeg.RunCommand(command, mediaId, ConversionPercentages, "hd", mediaInfo.Video.Duration); } //Convert Audio command = String.Format("-i \"{0}\" -vn -strict experimental -c:a aac -b:a 128k \"{1}\"", mediaDir, outputAudio); result = ffmpeg.RunCommand(command, mediaId, ConversionPercentages, "audio", mediaInfo.Video.Duration); //Extract thumbnail from the middle of the video command = String.Format(" -ss {0} -i \"{1}\" -vframes 1 -an -s 360x240 \"{2}\" ", (mediaInfo.Video.Duration / 2.0).ToString(CultureInfo.InvariantCulture), mediaDir, outputThumbnail); result = ffmpeg.RunCommand(command); if (mediaInfo.Audio.HasAudio) { //Convert to mobile (add sound to sd video) command = String.Format("-i \"{0}\" -i \"{1}\" -c:v copy -c:a copy \"{2}\"", outputVidSd, outputAudio, outputMobile); result = ffmpeg.RunCommand(command); //Convert to mobile Hd if (videoQuality != VideoQuality.p360) { command = String.Format("-i \"{0}\" -i \"{1}\" -c:v copy -c:a copy \"{2}\"", outputVidHd, outputAudio, outputMobileHd); result = ffmpeg.RunCommand(command); } } else { File.Copy(outputVidSd, outputMobile); if (videoQuality != VideoQuality.p360) { File.Copy(outputVidHd, outputMobileHd); } } //Segment videos and audio Mp4Box mp4Box = new Mp4Box(); if (videoQuality == VideoQuality.p360) { command = String.Format("-dash 2000 -frag 2000 -bs-switching no -segment-name \"%s_\" -url-template -out \"{0}\" \"{1}\" \"{2}\" ", Path.Combine(segmentsDir, "video.mpd"), outputVidSd, outputAudio); } else { command = String.Format("-dash 2000 -frag 2000 -bs-switching no -segment-name \"%s_\" -url-template -out \"{0}\" \"{1}\" \"{2}\" \"{3}\" ", Path.Combine(segmentsDir, "video.mpd"), outputVidSd, outputVidHd, outputAudio); } result = mp4Box.RunCommand(command); File.Delete(mediaDir); File.Delete(outputVidSd); if (File.Exists(outputVidHd)) { File.Delete(outputVidHd); } File.Delete(outputAudio); dbRecord.IsBeingConverted = false; db.Entry(dbRecord).State = EntityState.Modified; db.SaveChanges(); HttpContext.Current.Cache[mediaId.ToString()] = 1.0; }, TaskCreationOptions.LongRunning); }