[工具类]视频音频格式转换

写在前面

在终端越来越多的情况下,对媒体文件的要求就需要统一了,由于一些移动端的浏览器对flash兼容性不好,所以就需要考虑对视频或者音频格式进行转化了。

FFmpeg是一套可以用来记录、转换数字音频、视频,并能将其转化为流的开源计算机程序。采用LGPL或GPL许可证。它提供了录制、转换以及流化音视频的完整解决方案。它包含了非常先进的音频/视频编解码库libavcodec,为了保证高可移植性和编解码质量,libavcodec里很多codec都是从头开发的。

工具类

http://ffmpeg.org/download.html 可以去这里进行下载。

使用的时候,需要一下文件

    /// <summary>
    /// 转换工具类
    /// </summary>
    public static class FFmpegUtility
    {
        /// <summary>
        ///根目录
        /// </summary>
        public static string rootDirPath = AppDomain.CurrentDomain.BaseDirectory;
        /// <summary>
        /// ffmpeg路径
        /// </summary>
        private readonly static string _strFFmpegPath = Path.CombinerootDirPath, "ffmpeg.exe");
        /// <summary>
        /// 将音频转换为mp3的bat文件路径
        /// </summary>
        private readonly static string _audioToMp3BatPath = Path.CombinerootDirPath, "audiotomp3.bat");
        /// <summary>
        /// 获取视频第一帧图片的bat文件路径
        /// </summary>
        private readonly static string _videoImageBatPath = Path.CombinerootDirPath, "videotomp4.bat");
        /// <summary>
        /// 组装进程信息
        /// </summary>
        /// <returns></returns>
        private static void BuildProcessStartInfostring batPath, string sourPath, string savePath)
        {
            if string.IsNullOrEmptybatPath))
            {
                throw new ArgumentNullException"bat文件路径不正确");
            }
            if !File.ExistsbatPath))
            {
                throw new Exception"bat文件不存在");
            }
            ProcessStartInfo processStartInfo = new ProcessStartInfo);
            processStartInfo.FileName = batPath;
            processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            processStartInfo.Arguments = _strFFmpegPath + " " + sourPath + " " + savePath;
            processStartInfo.ErrorDialog = false;
            processStartInfo.CreateNoWindow = true;
            Process.StartprocessStartInfo);

        }
        /// <summary>
        /// 将音频文件转换为mp3
        /// </summary>
        public static void ConvertAudioToMp3OrMp4string sourcePath, string savePath)
        {
            if !File.Exists_audioToMp3BatPath))
            {
                File.WriteAllText_audioToMp3BatPath, "%1 -i %2 %3");
            }

            BuildProcessStartInfo_audioToMp3BatPath, sourcePath, savePath);
        }
        /// <summary>
        /// 将视频文件转换为MP4
        /// </summary>
        /// <param name="sourcePath">视频文件</param>
        /// <param name="savePath">图片保存的路径</param>
        /// <param name="width">图片宽度</param>
        /// <param name="height">图片高度</param>
        /// <returns></returns>
        public static string GetVideoImagestring sourcePath, string savePath, int width, int height)
        {
            if !File.Exists_videoImageBatPath))
            {
                File.WriteAllText_videoImageBatPath, "%1 -i %2 -y -f image2 -t 0.001 -s " + width + "x" + height + " %3");
            }
            BuildProcessStartInfo_videoImageBatPath, sourcePath, savePath);
            return savePath;
        }

    }

测试

    class Program
    {
        static void Mainstring[] args)
        {
            try
            {
                string exePath = AppDomain.CurrentDomain.BaseDirectory;
                string amrPath = Path.CombineexePath, "1.amr");
                string mp3Path = Path.CombineexePath, "23333333333333333.mp3");
                string mp4Path = Path.CombineexePath, "o5.mp4");
                string mp4ImgPath = Path.CombineexePath, "1111111111111111.png");
                FFmpegUtility.rootDirPath = exePath;
                FFmpegUtility.ConvertAudioToMp3OrMp4amrPath, mp3Path);
                FFmpegUtility.GetVideoImagemp4Path, mp4ImgPath, 500, 500);
                Console.WriteLine"转换成功");
            }
            catch Exception ex)
            {
                Console.WriteLineex.Message);
            }
            Console.Read);
        }

结果

总结

这里实现了amr转MP3,获取视频的缩略图。

Published by

风君子

独自遨游何稽首 揭天掀地慰生平