Rename MediaInfo() to GetMediaInfoString() to make it more sense
[marukotoolbox.git] / mp4box / Util.cs
blob8624599f337f9e375badc1233af52b629679d120
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;