首页 > 其他分享 >判断MP4 文件

判断MP4 文件

时间:2022-10-18 16:13:10浏览次数:40  
标签:文件 判断 string bytes Length MP4 bAll new byte

    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));
                }
            }
        }


标签:文件,判断,string,bytes,Length,MP4,bAll,new,byte
From: https://www.cnblogs.com/0624zfz/p/16802905.html

相关文章

  • C语言零基础入门-文件
    C语言零基础入门-文件这节课的主要内容:1,文件的基本知识。2,文件操作实例。1.基础知识1.1基础概念1.1.1文件这个概念不用说大家应该都知道是什么,虽然自己的定义可能不是很......
  • mybatis_17_根据properties元素的url属性指定的路径读取属性文件
    定义一个属性文件config.properties,文档结构示例:  文件内容示例:url=jdbc:postgresql://172.16.x.x:5432/database_name在mybatis-config2.xml的proeprties......
  • 记录上传文件到mysql数据库中遇到的问题
    在开发一个Qt的界面程序,想把程序批量生成的数据文件上传进数据库中。最开始尝试将文件读到QByteArray类型的变量中,插入数据表的mediumblob类型里,但是插不了,数据表结果一直......
  • mybatis_16_根据properties元素中的resource属性读取类路径下属性文件
    新建属性文件config.properties,文件结构如下:  在config.properties中定义属性,示例:url=jdbc:postgresql://172.16.x.x:5432/database_name在mybatis-config2.......
  • 傻瓜式midi文件转换成mp3软件分享!
    Midi音乐文件转MP3工具软件分享。mid格式是由MIDI继承而来。MID文件是记录声音的信息,然后告诉声卡如何再现音乐的一组指令。mid格式的最大用处是在电脑作曲领域。mid文件可......
  • mmdetection 生成c++ 的anchor头文件
    importosimportos.pathasospimportnumpyasnpfrommmcvimportConfigfrommmdet.modelsimportbuild_detectorimportmathimportargparseimportpickle......
  • malab 批处理将结果追加输出到csv文件中
    clcclearallfilename1='.\内蒙古自治区锡林郭勒盟典型草原轮牧放牧样地群落结构监测数据集(201.csv'%对fid=fopen('result3.txt','a+');%%B=readmatrix(fi......
  • 断点js-断点续传-大文件断点上传
    ​ javaweb上传文件上传文件的jsp中的部分上传文件同样可以使用form表单向后端发请求,也可以使用ajax向后端发请求    1.通过form表单向后端发送请求     ......
  • 视频格式文件种类
    .mp4;*.flv;*.f4v;*.webm;*.m4v;*.mov;*.3gp;*.3g2;*.rm;*.rmvb;*.wmv;*.avi;*.asf;*.mpg;*.mpeg:*.mpe;*.ts;*.div;*.dv;*.divx*.vob;*.dat;*.mkv;*.swf;*.lavf;*.cpk*.di......
  • Laravel 文件缓存场景 BUG
    起因在使用缓存场景,直接命令运行产生的缓存文件。与nginx+phpfpm运行产生的缓存文件,权限所属人不同,导致缓存文件无法使用。建议解决切换缓存引擎,将缓存引擎换为redis......