allow user to set time for OnePicAudio in General tab
[marukotoolbox.git] / mp4box / Util.cs
blobbc6a78b8617822dd2d64ac7eb482bf2fb2fe5dae
1 // ------------------------------------------------------------------
2 // Copyright (C) 2011-2016 Maruko Toolbox Project
3 //
4 // Authors: LYF <lyfjxymf@sina.com>
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 // You may obtain a copy of the License at
9 //
10 // http://www.apache.org/licenses/LICENSE-2.0
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
15 // express or implied.
16 // See the License for the specific language governing permissions
17 // and limitations under the License.
18 // -------------------------------------------------------------------
21 using System;
22 using System.Collections.Generic;
23 using System.Linq;
24 using System.Text;
25 using System.Diagnostics;
26 using System.Windows.Forms;
27 using System.IO;
28 using System.Threading;
29 using System.Net.NetworkInformation;
30 using System.Runtime.InteropServices;
32 namespace mp4box
34 class Util
36 /// <summary>
37 /// 自动加引号
38 /// </summary>
39 /// <param name="path"></param>
40 /// <returns></returns>
41 public static string FormatPath(string path)
43 if (string.IsNullOrEmpty(path)) { return null; }
44 string ret = null;
45 ret = path.Replace("\"", "");
46 if (ret.Contains(" ")) { ret = "\"" + ret + "\""; }
47 return ret;
50 /// <summary>
51 /// concat:D:\一二三123.png 可以防止FFMpeg不认中文名输入文件的Bug
52 /// </summary>
53 /// <param name="path"></param>
54 /// <returns></returns>
55 public static string ConcatProtocol(string path)
57 return FormatPath("concat:" + path);
60 /// <summary>
61 /// 防止文件或目录重名
62 /// </summary>
63 /// <param name="oriPath_">原路径</param>
64 /// <returns>不会重名的路径</returns>
65 public static string GetSuitablePath(string oriPath_)
67 if (string.IsNullOrEmpty(oriPath_)) { return null; }
68 string oriPath = oriPath_.Replace("\"", string.Empty);//去掉引号
70 if ((!File.Exists(oriPath)) && (!Directory.Exists(oriPath)))
72 return oriPath;
74 int i = 0;
75 string ext = Path.GetExtension(oriPath);
76 string str = oriPath.Remove(oriPath.Length - ext.Length) + "_" + i;
77 while (File.Exists(str + ext) || Directory.Exists(str + ext))
79 i++;
80 str = oriPath + "_" + i;
83 return str + ext;
86 /// <summary>
87 /// <para></para>输入:目标文件的 路径或所在目录;
88 /// <para></para>输入:原始文件的路径;
89 /// <para></para>输出:输出文件的路径。
90 /// <para></para>用目标目录或文件路径(取自tbOutput),和输入的文件,返回一个供输出的文件路径。
91 /// </summary>
92 /// <param name="DestDirOrFile_">目标目录或文件路径</param>
93 /// <param name="SrcFile_">输入的文件(若DestDirOrFile是文件,则忽略此项)</param>
94 /// <param name="dotExtension">换扩展名</param>
95 /// <returns></returns>
96 public static string GetSimilarFilePath(string DestDirOrFile_, string SrcFile_, string dotExtension)
98 if (string.IsNullOrEmpty(DestDirOrFile_)) { return null; }
99 if (string.IsNullOrEmpty(SrcFile_)) { return null; }
100 string DestDirOrFile = DestDirOrFile_.Replace("\"", string.Empty);
101 string SrcFile = SrcFile_.Replace("\"", string.Empty);//去掉引号
103 if (DestDirOrFile.EndsWith("\\"))//目录
105 if (string.IsNullOrEmpty(dotExtension))//没有指定扩展名
107 return DestDirOrFile + Path.GetFileName(SrcFile);
109 else//指定了扩展名
111 return DestDirOrFile + Path.GetFileNameWithoutExtension(SrcFile) + dotExtension;
114 else
116 //单文件,已经设置好了输出
117 //DestDirOrFile是文件路径
119 if (string.IsNullOrEmpty(dotExtension))//没有指定扩展名
121 return DestDirOrFile;
123 else//指定了扩展名
125 return ChangeExt(DestDirOrFile, dotExtension);//换扩展名
130 /// <summary>
131 /// 用目标目录或文件路径(取自tbOutput),和输入的文件,返回一个供输出的文件路径。
132 /// </summary>
133 /// <param name="DestDirOrFile">目标目录或文件路径</param>
134 /// <param name="SrcFile">输入的文件</param>
135 /// <returns></returns>
136 public static string GetSimilarFilePath(string DestDirOrFile, string SrcFile)
138 return GetSimilarFilePath(DestDirOrFile, SrcFile, null);
141 /// <summary>
142 /// 更换扩展名(保留绝对路径)
143 /// </summary>
144 /// <param name="srcFile"></param>
145 /// <param name="ext"></param>
146 /// <returns></returns>
147 public static string ChangeExt(string srcFile, string ext)
149 return GetDir(srcFile) + Path.GetFileNameWithoutExtension(srcFile) + ext;
152 /// <summary>
153 /// 获取文件目录,带“\”
154 /// </summary>
155 /// <param name="path">文件路径</param>
156 /// <returns></returns>
157 public static string GetDir(string path)
159 string fileDir = Path.GetDirectoryName(path);
160 if (fileDir.EndsWith("\\"))
162 return fileDir;
164 else
166 return fileDir + "\\";
170 public static void DeleteDirectoryIfExists(string path, bool recursive)
172 if (Directory.Exists(path))
173 Directory.Delete(path, recursive);
176 public static DirectoryInfo ensureDirectoryExists(string path)
178 if (Directory.Exists(path))
179 return new DirectoryInfo(path);
180 if (string.IsNullOrEmpty(path))
181 throw new IOException("无法创建目录");
182 ensureDirectoryExists(GetDirectoryName(path));
183 System.Threading.Thread.Sleep(100);
184 return Directory.CreateDirectory(path);
187 public static string GetDirectoryName(string file)
189 string path = string.Empty;
192 path = Path.GetDirectoryName(file);
194 catch { }
195 return path;
198 /// <summary>
199 /// Gets the file version/date
200 /// </summary>
201 /// <param name="fileName">the file to check</param>
202 /// <param name="fileVersion">the file version</param>
203 /// <param name="fileDate">the file date</param>
204 /// <param name="fileProductName">the file product name</param>
205 /// <returns>true if file can be found, false if file cannot be found</returns>
206 public static bool GetFileInformation(string fileName, out string fileVersion, out string fileDate, out string fileProductName)
208 fileVersion = fileDate = fileProductName = string.Empty;
209 if (!File.Exists(fileName))
210 return false;
212 FileVersionInfo FileProperties = FileVersionInfo.GetVersionInfo(fileName);
213 fileVersion = FileProperties.FileVersion;
214 if (!String.IsNullOrEmpty(fileVersion))
215 fileVersion = fileVersion.Replace(", ", ".");
216 fileDate = File.GetLastWriteTimeUtc(fileName).ToString("dd-MM-yyyy");
217 fileProductName = FileProperties.ProductName;
218 return true;
222 [DllImport("wininet.dll")]
223 private extern static bool InternetGetConnectedState(int Description, int ReservedValue);
224 /// <summary>
225 /// 用于检查网络是否可以连接互联网,true表示连接成功,false表示连接失败
226 /// </summary>
227 /// <returns></returns>
228 public static bool IsConnectInternet()
230 int Description = 0;
231 return InternetGetConnectedState(Description, 0);
234 /// <summary>
235 /// 检查目录是否可写入
236 /// </summary>
237 /// <param name"strPath">需要检查的路径</param>
238 public static bool IsDirWriteable(string strPath)
242 bool bDirectoryCreated = false;
244 if (!Directory.Exists(strPath))
246 Directory.CreateDirectory(strPath);
247 bDirectoryCreated = true;
250 string newFilePath = string.Empty;
252 newFilePath = Path.Combine(strPath, Path.GetRandomFileName());
253 while (File.Exists(newFilePath));
255 FileStream fs = File.Create(newFilePath);
256 fs.Close();
257 File.Delete(newFilePath);
259 if (bDirectoryCreated)
260 Directory.Delete(strPath);
262 return true;
264 catch
266 return false;
270 /// <summary>
271 /// Detects the AviSynth version/date
272 /// </summary>
273 /// <returns></returns>
274 public static string CheckAviSynth()
276 bool bFoundInstalledAviSynth = false;
277 string fileVersion = string.Empty, fileDate = string.Empty, fileProductName = string.Empty;
278 string syswow64path = Environment.GetFolderPath(Environment.SpecialFolder.SystemX86);
280 if (!Directory.Exists(syswow64path)
281 && GetFileInformation(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "avisynth.dll"), out fileVersion, out fileDate, out fileProductName))
282 bFoundInstalledAviSynth = true;
283 else if (GetFileInformation(Path.Combine(syswow64path, "avisynth.dll"), out fileVersion, out fileDate, out fileProductName))
284 bFoundInstalledAviSynth = true;
286 if (bFoundInstalledAviSynth)
287 return "AviSynth" + (fileProductName.Contains("+") ? "+" : string.Empty) + "版本: " + fileVersion + " (" + fileDate + ")";
288 else return string.Empty;
291 // 检查内置的avs版本
292 public static string CheckinternalAviSynth()
294 bool bFoundInstalledAviSynth = false;
295 string fileVersion = string.Empty, fileDate = string.Empty, fileProductName = string.Empty;
297 if (GetFileInformation(Path.Combine(Application.StartupPath, @"tools\AviSynth.dll"), out fileVersion, out fileDate, out fileProductName))
298 bFoundInstalledAviSynth = true;
300 if (bFoundInstalledAviSynth)
301 return "AviSynth" + (fileProductName.Contains("+") ? "+" : string.Empty) + "版本: " + fileVersion + " (" + fileDate + ")";
302 else return string.Empty;
305 #region DialogFilter
306 /// <summary>
307 /// Consolidate the Filter settings for OpenFileDialog & SaveFileDialog
308 /// </summary>
309 /// <param name="type">DialogFilterTypes</param>
310 /// <returns>string</returns>
311 public static string GetDialogFilter(DialogFilterTypes type)
313 string filterString;
314 //TODO: multi language support in the future
315 string all = "所有文件";
316 string avs = "AVS";
317 string video = "视频";
318 string audio = "音频";
319 string image = "图片";
320 string subtitle = "字幕";
321 string program = "程序";
322 string stream = "流";
324 switch (type)
326 //all
327 case DialogFilterTypes.ALL:
328 filterString = String.Format("{0}(*.*)|*.*;", all); break;
330 //video
331 case DialogFilterTypes.VIDEO_1:
332 filterString = String.Format("{0}(*.*)|*.*;", video); break;
333 case DialogFilterTypes.VIDEO_2:
334 filterString = String.Format("{0}(*.mkv)|*.mkv;", video); break;
335 case DialogFilterTypes.VIDEO_3:
336 filterString = String.Format("{0}(*.mp4)|*.mp4;", video); break;
337 case DialogFilterTypes.VIDEO_4:
338 filterString = String.Format("{0}(*.flv;*.hlv)|*.flv;*.hlv;", video); break;
339 case DialogFilterTypes.VIDEO_5:
340 filterString = String.Format("{0}(*.mp4)|*.mp4|{1}(*.*)|*.*;", video, all); break;
341 case DialogFilterTypes.VIDEO_6:
342 filterString = String.Format("{0}(*.mp4;*.flv;*.mkv)|*.mp4;*.flv;*.mkv|{1}(*.*)|*.*;", video, all); break;
343 case DialogFilterTypes.VIDEO_7:
344 filterString = String.Format("{0}(*.mp4;*.flv;*.mkv;*.wmv)|*.mp4;*.flv;*.mkv;*.wmv|{1}(*.*)|*.*;", video, all); break;
345 case DialogFilterTypes.VIDEO_8:
346 filterString = String.Format("{0}(*.mp4;*.flv;*.mkv;*.avi;*.wmv;*.mpg;*.avs)|*.mp4;*.flv;*.mkv;*.avi;*.wmv;*.mpg;*.avs|{1}(*.*)|*.*;", video, all); break;
347 case DialogFilterTypes.VIDEO_9:
348 filterString = String.Format("{0}(*.avi;*.mp4;*.m1v;*.m2v;*.m4v;*.264;*.h264;*.hevc)|*.avi;*.mp4;*.m1v;*.m2v;*.m4v;*.264;*.h264;*.hevc|{1}(*.*)|*.*;", video, all); break;
350 //video with detailed description
351 case DialogFilterTypes.VIDEO_D_1:
352 filterString = String.Format("FLV {0}(*.flv)|*.flv;", video); break;
353 case DialogFilterTypes.VIDEO_D_2:
354 filterString = String.Format("MP4 {0}(*.mp4)|*.mp4|FLV {0}(*.flv)|*.flv;", video); break;
355 case DialogFilterTypes.VIDEO_D_3:
356 filterString = String.Format("MPEG-4 {0}(*.mp4)|*.mp4|Flash {0}(*.flv)|*.flv|Matroska {0}(*.mkv)|*.mkv|AVI {0}(*.avi)|*.avi|H.264 {1}(*.raw)|*.raw;", video, stream); break;
358 //audio
359 case DialogFilterTypes.AUDIO_1:
360 filterString = String.Format("{0}(*.mp3)|*.mp3|{0}(*.aac)|*.aac|{1}(*.*)|*.*;", audio, all); break;
361 case DialogFilterTypes.AUDIO_2:
362 filterString = String.Format("{0}(*.mp3;*.aac;*.ac3)|*.mp3;*.aac;*.ac3|{1}(*.*)|*.*;", audio, all); break;
363 case DialogFilterTypes.AUDIO_3:
364 filterString = String.Format("{0}(*.aac;*.wav;*.m4a;*.flac)|*.aac;*.wav;*.m4a;*.flac;", audio); break;
365 case DialogFilterTypes.AUDIO_4:
366 filterString = String.Format("{0}(*.aac;*.mp3;*.mp4;*.wav)|*.aac;*.mp3;*.mp4;*.wav|{1}(*.*)|*.*;", audio, all); break;
367 case DialogFilterTypes.AUDIO_5:
368 filterString = String.Format("{0}(*.mp4;*.aac;*.mp2;*.mp3;*.m4a;*.ac3)|*.mp4;*.aac;*.mp2;*.mp3;*.m4a;*.ac3|{1}(*.*)|*.*;", audio, all); break;
370 //others
371 case DialogFilterTypes.AVS:
372 filterString = String.Format("{0}(*.avs)|*.avs;", avs); break;
373 case DialogFilterTypes.PROGRAM:
374 filterString = String.Format("{0}(*.exe)|*.exe|{1}(*.*)|*.*;", program, all); break;
375 case DialogFilterTypes.SUBTITLE_1:
376 filterString = String.Format("{0}(*.ass;*.ssa;*.srt)|*.ass;*.ssa;*.srt|{1}(*.*)|*.*;", subtitle, all); break;
377 case DialogFilterTypes.SUBTITLE_2:
378 filterString = String.Format("{0}(*.ass;*.ssa;*.srt;*.idx;*.sup)|*.ass;*.ssa;*.srt;*.idx;*.sup|{1}(*.*)|*.*;", subtitle, all); break;
379 case DialogFilterTypes.IMAGE:
380 filterString = String.Format("{0}(*.jpg;*.jpeg;*.png;*.bmp;*.gif)|*.jpg;*.jpeg;*.png;*.bmp;*.gif|{1}(*.*)|*.*;", image, all); break;
382 default:
383 filterString = String.Format("{0}(*.*)|*.*;", all); break;
385 return filterString;
388 public enum DialogFilterTypes
390 ALL,
391 VIDEO_1, VIDEO_2, VIDEO_3, VIDEO_4, VIDEO_5, VIDEO_6, VIDEO_7, VIDEO_8, VIDEO_9,
392 VIDEO_D_1, VIDEO_D_2, VIDEO_D_3,
393 AUDIO_1, AUDIO_2, AUDIO_3, AUDIO_4, AUDIO_5,
394 AVS,
395 PROGRAM,
396 SUBTITLE_1, SUBTITLE_2,
397 IMAGE,
400 #endregion