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

写在前面

在终端越来越多的情况下,对媒体文件的要求就需要统一了,由于一些移动端的浏览器对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.Combine(rootDirPath, "ffmpeg.exe");
        /// <summary>
        /// 将音频转换为mp3的bat文件路径
        /// </summary>
        private readonly static string _audioToMp3BatPath = Path.Combine(rootDirPath, "audiotomp3.bat");
        /// <summary>
        /// 获取视频第一帧图片的bat文件路径
        /// </summary>
        private readonly static string _videoImageBatPath = Path.Combine(rootDirPath, "videotomp4.bat");
        /// <summary>
        /// 组装进程信息
        /// </summary>
        /// <returns></returns>
        private static void BuildProcessStartInfo(string batPath, string sourPath, string savePath)
        {
            if (string.IsNullOrEmpty(batPath))
            {
                throw new ArgumentNullException("bat文件路径不正确");
            }
            if (!File.Exists(batPath))
            {
                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.Start(processStartInfo);

        }
        /// <summary>
        /// 将音频文件转换为mp3
        /// </summary>
        public static void ConvertAudioToMp3OrMp4(string 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 GetVideoImage(string 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 Main(string[] args)
        {
            try
            {
                string exePath = AppDomain.CurrentDomain.BaseDirectory;
                string amrPath = Path.Combine(exePath, "1.amr");
                string mp3Path = Path.Combine(exePath, "23333333333333333.mp3");
                string mp4Path = Path.Combine(exePath, "o5.mp4");
                string mp4ImgPath = Path.Combine(exePath, "1111111111111111.png");
                FFmpegUtility.rootDirPath = exePath;
                FFmpegUtility.ConvertAudioToMp3OrMp4(amrPath, mp3Path);
                FFmpegUtility.GetVideoImage(mp4Path, mp4ImgPath, 500, 500);
                Console.WriteLine("转换成功");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.Read();
        }

结果

总结

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

原文地址:https://www.cnblogs.com/wolf-sun/p/5048610.html