C# ffmpeg从视频画面中截取一帧保存图片

下载 ffmpeg 官网链接:http://ffmpeg.org/download.html

下载教程图片

C# ffmpeg从视频画面中截取一帧保存图片_第1张图片
C# ffmpeg从视频画面中截取一帧保存图片_第2张图片

配置教程图片

C# ffmpeg从视频画面中截取一帧保存图片_第3张图片
C# ffmpeg从视频画面中截取一帧保存图片_第4张图片

上传视频前端后台演示


<form method="post" action="提交到后台的Url" enctype="multipart/form-data">
    <input type="file" name="file" multiple="multiple" accept="video/*" />
    <br />
    <button type="submit">上传button>
form>

       //以下保存数据库的 原Url pictureUrl , 缩Url thumbnailUrl

		/// 
        /// 上传单个
        /// 
        /// 
        /// 
        [HttpPost]
        public ActionResult SaveOneFileVideo(HttpPostedFileBase file)
        {
            //保存相对路径
            string saveDir = $@"Data\Shipper\{ DateTime.Now.Year}\{ DateTime.Now.Month}\{ DateTime.Now.Day}";
            string thumbnailSaveDir = $@"{saveDir}\Thumbnail";

            //创建目录
            string fullSaveDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, saveDir);
            string fullThumbnailSaveDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, thumbnailSaveDir);
            if (!Directory.Exists(fullSaveDir))
            {
                Directory.CreateDirectory(fullSaveDir);
            }
            if (!Directory.Exists(fullThumbnailSaveDir))
            {
                Directory.CreateDirectory(fullThumbnailSaveDir);
            }

            //保存文件名
            string newFileName = Guid.NewGuid().ToString().Replace("-", "");
            var fileSuffix = Path.GetExtension(file.FileName);
            var fullFileName = newFileName + fileSuffix;

            //保存视频
            file.InputStream.Position = 0;
            var fullFilePath = Path.Combine(fullSaveDir, fullFileName);
            file.SaveAs(fullFilePath);
            string pictureUrl = VirtualPathUtility.Combine("/", Path.Combine(saveDir, fullFileName));


            //保存视频第一帧图片缩略图
            int cutTimeFrame = 1;
            string widthAndHeight = "128*72";
            fullFileName = fullFileName.Split('.')[0] + ".jpg";

            var fullThumbnailFilePath = Path.Combine(fullThumbnailSaveDir, fullFileName);

            //ffmpeg.exe路径
            var ffmpegPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"bin\ffmpeg.exe");

            var startInfo = new ProcessStartInfo(ffmpegPath);
            startInfo.WindowStyle = ProcessWindowStyle.Hidden;
            startInfo.Arguments = " -i " + fullFilePath + " -y -f image2 -ss " + cutTimeFrame + " -t 0.001 -s " + widthAndHeight + " " + fullThumbnailFilePath;
            Process.Start(startInfo);
            string thumbnailUrl = VirtualPathUtility.Combine("/", Path.Combine(thumbnailSaveDir, fullFileName));

            return View("Index");
        }


        /// 
        /// 上传多个
        /// 
        /// 
        [HttpPost]
        public ActionResult SaveMultipleFiles()
        {
            //保存相对路径
            string saveDir = $@"Data\Shipper\{ DateTime.Now.Year}\{ DateTime.Now.Month}\{ DateTime.Now.Day}";
            string thumbnailSaveDir = $@"{saveDir}\Thumbnail";

            //创建目录
            string fullSaveDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, saveDir);
            string fullThumbnailSaveDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, thumbnailSaveDir);
            if (!Directory.Exists(fullSaveDir))
            {
                Directory.CreateDirectory(fullSaveDir);
            }
            if (!Directory.Exists(fullThumbnailSaveDir))
            {
                Directory.CreateDirectory(fullThumbnailSaveDir);
            }

            HttpFileCollectionBase files = Request.Files;

            for (int i = 0; i < files.Count; i++)
            {
                var file = files[i];

                //保存文件名
                string newFileName = Guid.NewGuid().ToString().Replace("-", "");
                var fileSuffix = Path.GetExtension(file.FileName);
                var fullFileName = newFileName + fileSuffix;

                //保存视频
                file.InputStream.Position = 0;
                var fullFilePath = Path.Combine(fullSaveDir, fullFileName);
                file.SaveAs(fullFilePath);
                string pictureUrl = VirtualPathUtility.Combine("/", Path.Combine(saveDir, fullFileName));


                //保存视频第一帧图片缩略图
                int cutTimeFrame = 1;
                string widthAndHeight = "128*72";
                fullFileName = fullFileName.Split('.')[0] + ".jpg";

                var fullThumbnailFilePath = Path.Combine(fullThumbnailSaveDir, fullFileName);

                //ffmpeg.exe路径
                var ffmpegPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"bin\ffmpeg.exe");

                var startInfo = new ProcessStartInfo(ffmpegPath);
                startInfo.WindowStyle = ProcessWindowStyle.Hidden;
                startInfo.Arguments = " -i " + fullFilePath + " -y -f image2 -ss " + cutTimeFrame + " -t 0.001 -s " + widthAndHeight + " " + fullThumbnailFilePath;
                Process.Start(startInfo);
                string thumbnailUrl = VirtualPathUtility.Combine("/", Path.Combine(thumbnailSaveDir, fullFileName));
            }
            return View("Index");
        }

视频Base64格式

效果演示图片
C# ffmpeg从视频画面中截取一帧保存图片_第5张图片
前端页面

<body>
    <input type="file" id="file" name="file" accept="video/*" multiple="multiple" />
    <br />
    <br />
    <div id="Echo">
    
    div>
    <br />
    <button id="submitUpload">上传吧button>
body>
<script>
    $('#file').change(function () {
        var files = this.files;
        $.each(files, function (index, item) {
            //生成文件读取内置对象
            let fileReader = new FileReader();
            //等待文件阅读器读取完毕再渲染图片
            fileReader.onload = function () {
                $('#Echo').append(' this.result + '"data-name="' + item.name + '" />');
            }
            //页面添加元素 这是一个异步执行的过程,所以需要onload回调函数执行读取数据后的操作
            fileReader.readAsDataURL(item);
        });
        //清空,使得可以再次上传同一个
        $(this).val("");
    });

    $('#submitUpload').click(function () {
        $.each($('#Echo').children(), function (index, item) {
            var videoBase = $(item).find('source').attr('src');
            var videoFileName = $(item).find('source').attr('data-name');
            $.ajaxSettings.async = false;
            $.post('@Url.Action("SaveVideoBase")', { videoBase, videoFileName }, function (res) {

            });
        });
    });
</script>

后台代码


        /// 
        /// base64格式保存
        /// 
        /// base64视频
        /// 视频文件的名字,这样可以保存上传的原本格式
        /// 
        [HttpPost]
        public JsonResult SaveVideoBase(string videoBase, string videoFileName)
        {
            string base64 = videoBase.Substring(videoBase.IndexOf(',') + 1);
            byte[] base64array = Convert.FromBase64String(base64);

            //保存相对路径
            string saveDir = $@"Data\Shipper\{ DateTime.Now.Year}\{ DateTime.Now.Month}\{ DateTime.Now.Day}";
            string thumbnailSaveDir = $@"{saveDir}\Thumbnail";

            //创建目录
            string fullSaveDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, saveDir);
            string fullThumbnailSaveDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, thumbnailSaveDir);
            if (!Directory.Exists(fullSaveDir))
            {
                Directory.CreateDirectory(fullSaveDir);
            }
            if (!Directory.Exists(fullThumbnailSaveDir))
            {
                Directory.CreateDirectory(fullThumbnailSaveDir);
            }

            //保存文件名
            string newFileName = Guid.NewGuid().ToString().Replace("-", "");
            var fullFileName = newFileName + videoFileName;

            //保存原视频
            string fullFilePath = Path.Combine(fullSaveDir, fullFileName);
            System.IO.File.WriteAllBytes(fullFilePath, base64array);
            string pictureUrl = VirtualPathUtility.Combine("/", Path.Combine(saveDir, fullFileName));


            //保存视频第一帧图片缩略图
            int cutTimeFrame = 1;
            string widthAndHeight = "128*72";
            fullFileName = fullFileName.Split('.')[0] + ".jpg";

            var fullThumbnailFilePath = Path.Combine(fullThumbnailSaveDir, fullFileName);

            //ffmpeg.exe路径
            var ffmpegPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"bin\ffmpeg.exe");

            var startInfo = new ProcessStartInfo(ffmpegPath);
            startInfo.WindowStyle = ProcessWindowStyle.Hidden;
            startInfo.Arguments = " -i " + fullFilePath + " -y -f image2 -ss " + cutTimeFrame + " -t 0.001 -s " + widthAndHeight + " " + fullThumbnailFilePath;
            Process.Start(startInfo);
          
            string thumbnailUrl = VirtualPathUtility.Combine("/", Path.Combine(thumbnailSaveDir, fullFileName));

            return Json(new { }, JsonRequestBehavior.AllowGet);
        }

你可能感兴趣的:(C#,音视频,c#,mvc)