您好!欢迎来到默认站点! 请登录 注册有礼
首页 新闻资讯 > 研发日志 > C#

RTSP流转HLS流(二):ASP.NET启动ffmpeg进程拉流

2020-11-24 23:14:00 人评论

上一篇内容实现了通过命令行启动ffmpeg实现拉RTSP数据流并转码为m3u8文件,下面需要从ASP.NET应用程序开启进程来运行上节的ffmpeg命令。直接卸载Global.asax全局应用程序类中,需要将ffmpeg.exe文件复制到站点bin文件夹下using System;using System.Collections.Generic;using…

上一篇内容实现了通过命令行启动ffmpeg实现拉RTSP数据流并转码为m3u8文件,下面需要从ASP.NET应用程序开启进程来运行上节的ffmpeg命令。

直接卸载Global.asax全局应用程序类中,需要将ffmpeg.exe文件复制到站点bin文件夹下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Routing;
using System.Reflection;
using System.Diagnostics;
using System.IO;

namespace Mvcms.Web {
    public class MvcApplication : System.Web.HttpApplication {
        protected void Application_Start() {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            StartPull("upload/Media/01", "rtsp://admin:admin@192.168.1.64:554");
        }

        /// <summary>
        /// 开始拉视频流
        /// </summary>
        /// <param name="floderUrl">保存转码视频文件的相对路径</param>
        /// <param name="rtspUrl">RTSP视频流URL地址</param>
        /// <returns></returns>
        public int StartPull(string floderUrl, string rtspUrl) {
            int result = 0;
            string floderPath = Server.MapPath(floderUrl);
            if (!Directory.Exists(floderPath)) {
                Directory.CreateDirectory(floderPath);
            }
            Process process = null;
            string arguments  = string.Format(" -rtsp_transport tcp -i {0} -s 640x480 -force_key_frames \"expr:gte(t,n_forced*3)\" -c:v libx264 -hls_time 3 -hls_list_size 30 -hls_wrap 30 -f hls \"{1}\\play.m3u8\"", rtspUrl, floderPath);
            try {
                ProcessStartInfo processInfo = new ProcessStartInfo();
                processInfo.FileName = AppDomain.CurrentDomain.RelativeSearchPath + "\\ffmpeg.exe";
                processInfo.Arguments = arguments ;
                processInfo.CreateNoWindow = true;
                processInfo.UseShellExecute = false;
                processInfo.Verb = "RunAs";
                process = Process.Start(processInfo);
            }
            catch (Exception e) {
                if (process != null) {
                    process.Close();
                }
            }
            return result;
        }
    }
}

若成功启动进程,在站点/upload/media/01文件夹下会看到已完成保存的转码后的ts视频文件

图片.png

play.m3u8文件需要通过web视频播放组件播放,可以测试ts视频文件,在vs开启的浏览器键入如下地址可以通过http协议获取已生成的切片视频文件。

图片.png

下一步可以在web页面使用视频播放组件播放play.m3u8文件即可展现实时转换后的视频流

相关资讯

    暂无相关的数据...

共有条评论 网友评论

验证码: 看不清楚?
    会员登陆
    18004549898
    QQ
    Mvcms网站管理系统客服QQ:
    返回顶部