Improve UI in Mux tab.
[marukotoolbox.git] / mp4box / Cmd.cs
blob6f30998f780658f200555cede3121b2bcbd9ef01
1 // ------------------------------------------------------------------
2 // Copyright (C) 2011-2015 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;
30 namespace mp4box
32 class Cmd
35 /// <summary>
36 /// 自动加引号
37 /// </summary>
38 /// <param name="path"></param>
39 /// <returns></returns>
40 public static string FormatPath(string path)
42 if (string.IsNullOrEmpty(path)) { return null; }
43 string ret = null;
44 ret = path.Replace("\"", "");
45 if (ret.Contains(" ")) { ret = "\"" + ret + "\""; }
46 return ret;
49 /// <summary>
50 /// concat:D:\一二三123.png 可以防止FFMpeg不认中文名输入文件的Bug
51 /// </summary>
52 /// <param name="path"></param>
53 /// <returns></returns>
54 public static string ConcatProtocol(string path)
56 return FormatPath("concat:" + path);
59 /// <summary>
60 /// 防止文件或目录重名
61 /// </summary>
62 /// <param name="oriPath_">原路径</param>
63 /// <returns>不会重名的路径</returns>
64 public static string GetSuitablePath(string oriPath_)
66 if (string.IsNullOrEmpty(oriPath_)) { return null; }
67 string oriPath = oriPath_.Replace("\"", string.Empty);//去掉引号
69 if ((!File.Exists(oriPath)) && (!Directory.Exists(oriPath)))
71 return oriPath;
73 int i = 0;
74 string ext = Path.GetExtension(oriPath);
75 string str = oriPath.Remove(oriPath.Length - ext.Length) + "_" + i;
76 while (File.Exists(str + ext) || Directory.Exists(str + ext))
78 i++;
79 str = oriPath + "_" + i;
82 return str + ext;
84 /// <summary>
85 /// <para></para>输入:目标文件的 路径或所在目录;
86 /// <para></para>输入:原始文件的路径;
87 /// <para></para>输出:输出文件的路径。
88 /// <para></para>用目标目录或文件路径(取自tbOutput),和输入的文件,返回一个供输出的文件路径。
89 /// </summary>
90 /// <param name="DestDirOrFile_">目标目录或文件路径</param>
91 /// <param name="SrcFile_">输入的文件(若DestDirOrFile是文件,则忽略此项)</param>
92 /// <param name="dotExtension">换扩展名</param>
93 /// <returns></returns>
94 public static string GetSimilarFilePath(string DestDirOrFile_, string SrcFile_, string dotExtension)
96 if (string.IsNullOrEmpty(DestDirOrFile_)) { return null; }
97 if (string.IsNullOrEmpty(SrcFile_)) { return null; }
98 string DestDirOrFile = DestDirOrFile_.Replace("\"", string.Empty);
99 string SrcFile = SrcFile_.Replace("\"", string.Empty);//去掉引号
101 if (DestDirOrFile.EndsWith("\\"))//目录
103 if (string.IsNullOrEmpty(dotExtension))//没有指定扩展名
105 return DestDirOrFile + Path.GetFileName(SrcFile);
107 else//指定了扩展名
109 return DestDirOrFile + Path.GetFileNameWithoutExtension(SrcFile) + dotExtension;
112 else
114 //单文件,已经设置好了输出
115 //DestDirOrFile是文件路径
117 if (string.IsNullOrEmpty(dotExtension))//没有指定扩展名
119 return DestDirOrFile;
121 else//指定了扩展名
123 return ChangeExt(DestDirOrFile, dotExtension);//换扩展名
127 /// <summary>
128 /// 用目标目录或文件路径(取自tbOutput),和输入的文件,返回一个供输出的文件路径。
129 /// </summary>
130 /// <param name="DestDirOrFile">目标目录或文件路径</param>
131 /// <param name="SrcFile">输入的文件</param>
132 /// <returns></returns>
133 public static string GetSimilarFilePath(string DestDirOrFile, string SrcFile)
135 return GetSimilarFilePath(DestDirOrFile, SrcFile, null);
137 /// <summary>
138 /// 更换扩展名(保留绝对路径)
139 /// </summary>
140 /// <param name="srcFile"></param>
141 /// <param name="ext"></param>
142 /// <returns></returns>
143 public static string ChangeExt(string srcFile, string ext)
145 return GetDir(srcFile) + Path.GetFileNameWithoutExtension(srcFile) + ext;
147 /// <summary>
148 /// 获取文件目录,带“\”
149 /// </summary>
150 /// <param name="path">文件路径</param>
151 /// <returns></returns>
152 public static string GetDir(string path)
154 string fileDir = Path.GetDirectoryName(path);
155 if (fileDir.EndsWith("\\"))
157 return fileDir;
159 else
161 return fileDir + "\\";