private void TextLog(VideoMp4 item) { if (this.Dispatcher.CheckAccess()) { //ignore } else { this.Dispatcher.Invoke(new Action(delegate { LogTextBox.Text += "转换完成,生成文件" + item.NewFileName + "\n"; LogTextBox.Text += "文件路径为" + item.NewFilePath + "\n"; LogHelper.AddEventLog("转换完成,生成文件" + item.NewFileName + "\n"); LogHelper.AddEventLog("文件路径为" + item.NewFilePath + "\n"); })); } }
private void DeepFileDir(DirectoryInfo theDir) //递归目录 文件 { FileInfo[] fileInfo = theDir.GetFiles(); //目录下的文件 foreach (FileInfo fInfo in fileInfo) { if (!(list0.Contains(fInfo.Name)) && fInfo.Extension == ".mp4") { var oldFilePath = fInfo.FullName; var newdir = ""; var newPath = ""; var newFilePath = ""; if (TabControl0.SelectedIndex == 0) { //转换为m3u8 newdir = fInfo.DirectoryName.Replace(FileDir.Text, NewFileDir.Text); newPath = newdir + "\\" + fInfo.Name.Replace(fInfo.Extension, "") + "\\"; newFilePath = newPath + fInfo.Name.Replace(fInfo.Extension, "") + ".m3u8"; } else if (TabControl0.SelectedIndex == 1) { newdir = fInfo.DirectoryName.Replace(FileDir1.Text, NewFileDir1.Text); newPath = newdir + "\\"; newFilePath = newPath + fInfo.Name.Replace(fInfo.Extension, "") + ".mp4"; } else if (TabControl0.SelectedIndex == 2) { //添加水印后仍然为mp4视频 newdir = fInfo.DirectoryName.Replace(FileDir2.Text, NewFileDir2.Text); newPath = newdir + "\\"; newFilePath = newPath + fInfo.Name.Replace(fInfo.Extension, "") + ".mp4"; } var item = new VideoMp4(); if (TabControl0.SelectedIndex == 0) { //转换为m3u8 item.CmdString = string.Format("-i \"{0}\" -hls_time 5 -hls_list_size 0 -c:v libx264 -c:a aac -strict -2 -threads {1} -f hls \"{2}\"", oldFilePath, cpuCount, newFilePath); } else if (TabControl0.SelectedIndex == 1) { //添加元信息为第一帧 item.CmdString = string.Format("\"{0}\" \"{1}\"", oldFilePath, newFilePath); } else if (TabControl0.SelectedIndex == 2) { //添加水印 这个图片写成绝对路径 不会被识别,只能放到和exe执行文件同一目录下 //示例:ffmpeg -y –i "C:\Users\Administrator\Desktop\watermark\am.mp4" -vf "movie=cnkiwatermark.png,scale=135:50[watermark];[in][watermark]overlay=main_w-overlay_w-10:10[out]" C:\Users\Administrator\Desktop\watermark2\am.mp4 item.CmdString = string.Format(" -y -i \"{0}\" -vf \"movie={1},scale=170:60[watermark];[in][watermark]overlay=main_w-overlay_w-10:10[out]\" \"{2}\"", oldFilePath, waterMarkImg, newFilePath); } item.OldfileName = fInfo.Name; item.OldFilePath = fInfo.FullName; if (TabControl0.SelectedIndex == 0) { item.NewFileName = fInfo.Name.Replace(fInfo.Extension, "") + ".m3u8"; } else { item.NewFileName = fInfo.Name.Replace(fInfo.Extension, "") + ".mp4"; } item.NewFilePath = newFilePath; ListQueue.Enqueue(item); //创建目录 if (!Directory.Exists(System.IO.Path.GetDirectoryName(newPath))) { Directory.CreateDirectory(newPath); } } } DirectoryInfo[] subDirectories = theDir.GetDirectories();//子目录 foreach (DirectoryInfo dirinfo in subDirectories) { DeepFileDir(dirinfo); } }
private void StartOnXml_OnClick(object sender, RoutedEventArgs e) { if (TabControl0.SelectedIndex == 2 && string.IsNullOrEmpty(waterMarkImg)) { System.Windows.Forms.MessageBox.Show("请选择要添加水印的图片", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } //获取设置 cpuCount = AppConfigHelper.GetAppConfigInt("CpuCount"); threadCount = AppConfigHelper.GetAppConfigInt("ThreadCount"); //清空当前列表 ListQueue.Clear(); //清空进度条 this.pb.Maximum = 0; this.tb.Text = "0%"; this.pb.Value = 0; //先从根目录下查找xml XmlDocument xmldoc = new XmlDocument(); try { string xmlPath = System.AppDomain.CurrentDomain.BaseDirectory + "watermark.xml"; xmldoc.Load(xmlPath); } catch (Exception ex) { System.Windows.Forms.MessageBox.Show("xml文件加载出错", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); LogHelper.AddErrorLog(ex.ToString()); return; } //获取节点列表 XmlNodeList topNode = xmldoc.DocumentElement.ChildNodes; foreach (XmlElement element in topNode) { var str = element.InnerText; var item = new VideoMp4(); var oldFilePath = str; // newFilePath.Substring(0,newFilePath.LastIndexOf(".")) var filename = oldFilePath.Substring(oldFilePath.LastIndexOf("\\")).Replace("\\", ""); var fileformat = filename.Substring(filename.LastIndexOf(".")); var newfilename = filename.Substring(0, filename.LastIndexOf(".")) + "_znykt_cnki" + fileformat; var newFilePath = oldFilePath.Substring(0, oldFilePath.LastIndexOf("\\")) + "\\" + newfilename; item.CmdString = string.Format(" -y -i \"{0}\" -vf \"movie={1},scale=170:60[watermark];[in][watermark]overlay=main_w-overlay_w-10:10[out]\" \"{2}\"", oldFilePath, waterMarkImg, newFilePath); item.NewFileName = newfilename; item.NewFilePath = newFilePath; item.OldFilePath = oldFilePath; item.OldfileName = filename; ListQueue.Enqueue(item); } if (ListQueue.Count == 0) { System.Windows.Forms.MessageBox.Show("该路径下未找到MP4格式的视频", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } LogTextBox.Clear(); LogTextBox.Text = "该目录下共找到 " + ListQueue.Count + " 个视频,开始进行转换......\n"; iAmount = ListQueue.Count; iCount = 0; iNow = 0; Thread thread = new Thread(ThreadStart); thread.IsBackground = true; thread.Start(); Select.IsEnabled = false; Start.IsEnabled = false; Select1.IsEnabled = false; Start1.IsEnabled = false; pb.Maximum = ListQueue.Count; }