public class CheckResult
{
public bool IsMp4 { get; set; }
public bool SupportStreaming { get; set; }
}
/// <summary>
/// 判断是否Mp4及Moov前置
/// </summary>
/// <param name="mediaFile"></param>
/// <returns></returns>
public static CheckResult CheckMp4AndMoov(string mediaFile)
{
#region
//how to: c# 判断moov是否在mdat之前??
/*
mp4文件需要有ftyp, moov, mdat, 它们都是顶级Atom,不能被其他Atom嵌套。
ftyp 标示了MP4文件, 必须出现在第一个.
moov 保存了视频的基本信息.
mdat 保存视频和音频数据,这两个Atom顺序不固定。
*/
bool isMp4 = false;
bool isStreaming = false;
using (Stream fs = new FileStream(mediaFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
byte[] bytes = new byte[500];
fs.Position = 0;
int readLen = fs.Read(bytes, 0, bytes.Length);
/*char[] cc = Encoding.Default.GetChars(bytes);
StringBuilder sb = new StringBuilder();
foreach (char c in cc)
{
string s1 = c.ToString();
if (s1 == "\0")
{
continue;
}
sb.Append(s1);
}
res = sb.ToString();*/
//以下转换后再判断不准确!!! 应该直接查询原始bytes数组中的数据
//isMp4 = res.IndexOf("ftyp") >= 0;
//moovPos = res.IndexOf("moov");
//mdatPos = res.IndexOf("mdat");
//ASCII
//https://baike.baidu.com/item/ASCII/309296?fr=kg_general
//check if 'moov' at the begining, if yes, then ignore convert
int moovPos = GetIndexOf(bytes, new byte[] { 0x6d, 0x6f, 0x6f, 0x76, 0x0, 0x0, 0x0 });
//check "mdat"
int mdatPos = GetIndexOf(bytes, new byte[] { 0x6D, 0x64, 0x61, 0x74 });
//check "ftyp" for whether is mp4 file
int ftypPos = GetIndexOf(bytes, new byte[] { 0x66, 0x74, 0x79, 0x70 });
isMp4 = ftypPos >= 0;
if (isMp4)
{
if (moovPos > 0 && mdatPos < 0)
{
isStreaming = true;
}
else if (moovPos > 0 && mdatPos > 0 && moovPos < mdatPos)
{
isStreaming = true;
}
}
}
return new CheckResult
{
IsMp4 = isMp4,
SupportStreaming = isStreaming
};
#endregion
}
/// <summary>
/// 检测查询的字符是否连续存在
/// </summary>
/// <param name="bAll"></param>
/// <param name="bCheck"></param>
/// <param name="from"></param>
/// <returns></returns>
private static int GetIndexOf(byte[] bAll, byte[] bCheck, int from = 0)
{
#region
if (bAll == null || bCheck == null || bAll.Length == 0 || bCheck.Length == 0)
{
return -1;
}
int i, j;
for (i = from; i < bAll.Length; i++)
{
if (bAll[i] == bCheck[0])
{
for (j = 1; j < bCheck.Length; j++)
{
if (i + j >= bAll.Length)
{
break;
}
if (bAll[i + j] != bCheck[j])
{
break;
}
}
if (j == bCheck.Length)
{
return i;
}
}
}
return -1;
#endregion
}
/// <summary>
/// h264 format check
/// </summary>
/// <param name="file"></param>
/// <returns></returns>
public static bool IsH264Mp4(string file)
{
#region
bool isH264 = false;
try
{
/*var ffP = new NReco.VideoInfo.FFProbe();
var vInfo = ffP.GetMediaInfo(file);
var s = vInfo.Streams;
foreach (var item in s)
{
if (item.CodecType == "video" && item.CodecName == "h264")
{
isH264 = true;
break;
}
}*/
var mediaFile = new MediaFile(file);
foreach (var vid in mediaFile.Video)
{
string type = vid.InternetMediaType + "";
isH264 = type.ToLower().IndexOf("h264") >= 0;
if (isH264)
{
break;
}
}
}
catch { }
return isH264;
#endregion
}
public static void KillProcess(string processName)
{
if (string.IsNullOrEmpty(processName))
{
return;
}
processName = Path.GetFileNameWithoutExtension(processName).ToLower();
foreach (Process p in Process.GetProcesses())
{
if (p.ProcessName.ToLower() != processName)
{
continue;
}
try
{
string cmdLine = p.GetCommandLineArgs();
if (cmdLine.IndexOf("MP4ConversionTool") > 0)
{
p.Kill();
p.WaitForExit();
}
}
catch (Exception ex)
{
Console.WriteLine(string.Format("Error:{0}\n{1}", ex.Message, ex.StackTrace));
}
}
}
{
public bool IsMp4 { get; set; }
public bool SupportStreaming { get; set; }
}
/// <summary>
/// 判断是否Mp4及Moov前置
/// </summary>
/// <param name="mediaFile"></param>
/// <returns></returns>
public static CheckResult CheckMp4AndMoov(string mediaFile)
{
#region
//how to: c# 判断moov是否在mdat之前??
/*
mp4文件需要有ftyp, moov, mdat, 它们都是顶级Atom,不能被其他Atom嵌套。
ftyp 标示了MP4文件, 必须出现在第一个.
moov 保存了视频的基本信息.
mdat 保存视频和音频数据,这两个Atom顺序不固定。
*/
bool isMp4 = false;
bool isStreaming = false;
using (Stream fs = new FileStream(mediaFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
byte[] bytes = new byte[500];
fs.Position = 0;
int readLen = fs.Read(bytes, 0, bytes.Length);
/*char[] cc = Encoding.Default.GetChars(bytes);
StringBuilder sb = new StringBuilder();
foreach (char c in cc)
{
string s1 = c.ToString();
if (s1 == "\0")
{
continue;
}
sb.Append(s1);
}
res = sb.ToString();*/
//以下转换后再判断不准确!!! 应该直接查询原始bytes数组中的数据
//isMp4 = res.IndexOf("ftyp") >= 0;
//moovPos = res.IndexOf("moov");
//mdatPos = res.IndexOf("mdat");
//ASCII
//https://baike.baidu.com/item/ASCII/309296?fr=kg_general
//check if 'moov' at the begining, if yes, then ignore convert
int moovPos = GetIndexOf(bytes, new byte[] { 0x6d, 0x6f, 0x6f, 0x76, 0x0, 0x0, 0x0 });
//check "mdat"
int mdatPos = GetIndexOf(bytes, new byte[] { 0x6D, 0x64, 0x61, 0x74 });
//check "ftyp" for whether is mp4 file
int ftypPos = GetIndexOf(bytes, new byte[] { 0x66, 0x74, 0x79, 0x70 });
isMp4 = ftypPos >= 0;
if (isMp4)
{
if (moovPos > 0 && mdatPos < 0)
{
isStreaming = true;
}
else if (moovPos > 0 && mdatPos > 0 && moovPos < mdatPos)
{
isStreaming = true;
}
}
}
return new CheckResult
{
IsMp4 = isMp4,
SupportStreaming = isStreaming
};
#endregion
}
/// <summary>
/// 检测查询的字符是否连续存在
/// </summary>
/// <param name="bAll"></param>
/// <param name="bCheck"></param>
/// <param name="from"></param>
/// <returns></returns>
private static int GetIndexOf(byte[] bAll, byte[] bCheck, int from = 0)
{
#region
if (bAll == null || bCheck == null || bAll.Length == 0 || bCheck.Length == 0)
{
return -1;
}
int i, j;
for (i = from; i < bAll.Length; i++)
{
if (bAll[i] == bCheck[0])
{
for (j = 1; j < bCheck.Length; j++)
{
if (i + j >= bAll.Length)
{
break;
}
if (bAll[i + j] != bCheck[j])
{
break;
}
}
if (j == bCheck.Length)
{
return i;
}
}
}
return -1;
#endregion
}
/// <summary>
/// h264 format check
/// </summary>
/// <param name="file"></param>
/// <returns></returns>
public static bool IsH264Mp4(string file)
{
#region
bool isH264 = false;
try
{
/*var ffP = new NReco.VideoInfo.FFProbe();
var vInfo = ffP.GetMediaInfo(file);
var s = vInfo.Streams;
foreach (var item in s)
{
if (item.CodecType == "video" && item.CodecName == "h264")
{
isH264 = true;
break;
}
}*/
var mediaFile = new MediaFile(file);
foreach (var vid in mediaFile.Video)
{
string type = vid.InternetMediaType + "";
isH264 = type.ToLower().IndexOf("h264") >= 0;
if (isH264)
{
break;
}
}
}
catch { }
return isH264;
#endregion
}
public static void KillProcess(string processName)
{
if (string.IsNullOrEmpty(processName))
{
return;
}
processName = Path.GetFileNameWithoutExtension(processName).ToLower();
foreach (Process p in Process.GetProcesses())
{
if (p.ProcessName.ToLower() != processName)
{
continue;
}
try
{
string cmdLine = p.GetCommandLineArgs();
if (cmdLine.IndexOf("MP4ConversionTool") > 0)
{
p.Kill();
p.WaitForExit();
}
}
catch (Exception ex)
{
Console.WriteLine(string.Format("Error:{0}\n{1}", ex.Message, ex.StackTrace));
}
}
}
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决