简体   繁体   English

使用 FFMPEG 的 ASP.NET Core 5.0 MVC HLS 转码

[英]ASP.NET Core 5.0 MVC HLS Transcoding using FFMPEG

Overview概述

I'm currently working on a media streaming server using ASP.net Core REST Server.我目前正在使用 ASP.net Core REST Server 开发媒体流服务器。 I'm currently using .net 5.0 and ASP.net Core MVC我目前正在使用 .net 5.0 和 ASP.net Core MVC

What I need我需要的

I need to be able to dynamically down-res the original video file.我需要能够动态降低原始视频文件的分辨率。 from 1080p to 720p for example.例如从 1080p 到 720p。 Also I need to be able to make the media file able to be transcoded to a different encoding based on client capabilities.此外,我还需要能够根据客户端功能将媒体文件转码为不同的编码。

What I've Tried我试过的

I've been looking for a library that can manage this feat, but I can't seem to find one.我一直在寻找可以管理这一壮举的图书馆,但我似乎找不到。 I thought FFMpeg would be able to do this.我认为 FFMpeg 能够做到这一点。 I know this is possible because applications like plex and emby seem to manage this.'我知道这是可能的,因为像 plex 和 emby 这样的应用程序似乎可以管理这个。

C# C#

public static FileStream GetTranscodedStream(string requestedUser, string path, int targetResolution, int targetBitRate)
{
    string directoryOutput = Directory.CreateDirectory(Path.Combine(Paths.TempData, $"stream_{requestedUser}")).FullName;
    string fileOutput = Path.Combine(directoryOutput, $"v_t{path}-r{targetResolution}.m3u8");
    string exe = Directory.GetFiles(Paths.FFMpeg, "ffmpeg*", SearchOption.AllDirectories)[0];
    string arguments = $"-i \"{media.PATH}\" -bitrate {targetBitRate}k -f hls -hls_time 2 -hls_playlist_type vod -hls_flags independent_segments -hls_segment_type mpegts -hls_segment_filename \"{Path.Combine(directoryOutput, $"stream_t{path}-r{targetResolution}%02d.ts")}\" \"{fileOutput}\"";
    Process process = new()
    {
        StartInfo = new()
        {
            FileName = exe,
            Arguments = arguments,
            UseShellExecute = true,
        },
        EnableRaisingEvents = true,
    };
    process.Start();
    return new(fileOutput, FileMode.Open);
}

[HttpGet("{tmdb}/{user}/video/transcoded")]
public IActionResult GetMovieStream(string tmdb, string user, int resolution, int bitrate)
{
    MediaBase movie = MovieLibraryModel.Instance.GetMovieByTMDB(tmdb);
    var transcoded = GetTranscodedStream(user, movie.PATH, resolution, bitrate);
    long fileSize = new FileInfo(movie.PATH).Length;
    Response.Headers.Clear();
    Response.ContentLength = fileSize;
    Response.Headers.Add("Accept-Ranges", $"bytes");
    Response.Headers.Add("Content-Range", $"bytes {0}-{fileSize}/{fileSize}");
    activeStreams.Add(Users.Instance.Get(user), timer);
    return File(transcoded, "application/x-mpegURL", true);
}

HTML HTML

<link rel="stylesheet" href="/assets/lib/video.js/video-js.css">
    <video id="vid1" class="videojs vjs-default-skin" controls data-setup="{}" preload="auto">
        <source src="http://127.0.0.1:3208/api/get/movies/299687/dcman58/video/transcoded?resolution=480&bitrate=4800" type="application/x-mpegURL">
    </video>

Javascript Javascript

var player = videojs('vid1');
player.play();

ERROR错误

ERROR 416: Range Not Satisfiable "http://127.0.0.1:3208/api/get/movies/299687/dcman58/video/transcoded?resolution=480&bitrate=4800"

Your problem is with this line:您的问题出在这一行:

Response.Headers.Add("Content-Range", $"bytes {0}-{fileSize}/{fileSize}");

This is the error description: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/416这是错误描述: https : //developer.mozilla.org/en-US/docs/Web/HTTP/Status/416

I suspect you're approaching the problem wrong.我怀疑你处理问题的方式有误。 The FFMPEG encoding needs to run to completion before you can start serving any data back to the client. FFMPEG 编码需要运行完成,然后才能开始向客户端提供任何数据。 You cant retrieve the output from FFMPEG on the fly.您无法即时检索 FFMPEG 的输出。

I can't tell from your code whether the files are already converted and on disk at the point that you're trying to return them;在您尝试返回文件时,我无法从您的代码中判断文件是否已经转换并在磁盘上; if so, just return the file itself without specifying ranges, the "application/x-mpegURL" part in your return will do the magic for you如果是这样,只需返回文件本身而不指定范围,返回中的"application/x-mpegURL"部分将为您带来魔力

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM