首页 > 解决方案 > FFMPEG 生成的 MPEG Dash 输出不起作用

问题描述

我使用 ffmpeg 创建了一个破折号输出 sample_video.mpd 文件,但是当我尝试使用破折号 js 播放它时,视频只是在加载,没有别的。下面是生成的mpd文件

<?xml version="1.0" encoding="utf-8"?>
<MPD xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="urn:mpeg:dash:schema:mpd:2011"
xmlns:xlink="http://www.w3.org/1999/xlink"
xsi:schemaLocation="urn:mpeg:DASH:schema:MPD:2011 http://standards.iso.org/ittf/PubliclyAvailableStandards/MPEG-DASH_schema_files/DASH-MPD.xsd"
profiles="urn:mpeg:dash:profile:isoff-live:2011"
type="static"
mediaPresentationDuration="PT1M2.3S"
minBufferTime="PT20.0S">
<ProgramInformation>
</ProgramInformation>
<Period id="0" start="PT0.0S">
    <AdaptationSet id="0" contentType="video" segmentAlignment="true" bitstreamSwitching="true" lang="und">
        <Representation id="0" mimeType="video/mp4" codecs="avc1.640015" bandwidth="255520" width="426" height="240" frameRate="24/1">
            <SegmentTemplate timescale="12288" initialization="init-stream$RepresentationID$.m4s" media="chunk-stream$RepresentationID$-$Number%05d$.m4s" startNumber="3">
                <SegmentTimeline>
                    <S t="198656" d="69120" />
                    <S d="75264" />
                    <S d="174592" />
                    <S d="122880" />
                    <S d="125440" />
                </SegmentTimeline>
            </SegmentTemplate>
        </Representation>
        <Representation id="2" mimeType="video/mp4" codecs="avc1.64001e" bandwidth="726596" width="854" height="480" frameRate="24/1">
            <SegmentTemplate timescale="12288" initialization="init-stream$RepresentationID$.m4s" media="chunk-stream$RepresentationID$-$Number%05d$.m4s" startNumber="3">
                <SegmentTimeline>
                    <S t="198656" d="69120" />
                    <S d="75264" />
                    <S d="174592" />
                    <S d="122880" />
                    <S d="125440" />
                </SegmentTimeline>
            </SegmentTemplate>
        </Representation>
        <Representation id="4" mimeType="video/mp4" codecs="avc1.64001f" bandwidth="1433314" width="1280" height="720" frameRate="24/1">
            <SegmentTemplate timescale="12288" initialization="init-stream$RepresentationID$.m4s" media="chunk-stream$RepresentationID$-$Number%05d$.m4s" startNumber="3">
                <SegmentTimeline>
                    <S t="198656" d="69120" />
                    <S d="75264" />
                    <S d="174592" />
                    <S d="122880" />
                    <S d="125440" />
                </SegmentTimeline>
            </SegmentTemplate>
        </Representation>
    </AdaptationSet>
    <AdaptationSet id="1" contentType="audio" segmentAlignment="true" bitstreamSwitching="true" lang="und">
        <Representation id="1" mimeType="audio/mp4" codecs="mp4a.40.2" bandwidth="341000" audioSamplingRate="48000">
            <AudioChannelConfiguration schemeIdUri="urn:mpeg:dash:23003:3:audio_channel_configuration:2011" value="6" />
            <SegmentTemplate timescale="48000" initialization="init-stream$RepresentationID$.m4s" media="chunk-stream$RepresentationID$-$Number%05d$.m4s" startNumber="3">
                <SegmentTimeline>
                    <S t="772096" d="270336" />
                    <S d="293888" />
                    <S d="681984" />
                    <S d="480256" />
                    <S d="492544" />
                </SegmentTimeline>
            </SegmentTemplate>
        </Representation>
        <Representation id="3" mimeType="audio/mp4" codecs="mp4a.40.2" bandwidth="341000" audioSamplingRate="48000">
            <AudioChannelConfiguration schemeIdUri="urn:mpeg:dash:23003:3:audio_channel_configuration:2011" value="6" />
            <SegmentTemplate timescale="48000" initialization="init-stream$RepresentationID$.m4s" media="chunk-stream$RepresentationID$-$Number%05d$.m4s" startNumber="3">
                <SegmentTimeline>
                    <S t="772096" d="270336" />
                    <S d="293888" />
                    <S d="681984" />
                    <S d="480256" />
                    <S d="492544" />
                </SegmentTimeline>
            </SegmentTemplate>
        </Representation>
        <Representation id="5" mimeType="audio/mp4" codecs="mp4a.40.2" bandwidth="341000" audioSamplingRate="48000">
            <AudioChannelConfiguration schemeIdUri="urn:mpeg:dash:23003:3:audio_channel_configuration:2011" value="6" />
            <SegmentTemplate timescale="48000" initialization="init-stream$RepresentationID$.m4s" media="chunk-stream$RepresentationID$-$Number%05d$.m4s" startNumber="3">
                <SegmentTimeline>
                    <S t="772096" d="270336" />
                    <S d="293888" />
                    <S d="681984" />
                    <S d="480256" />
                    <S d="492544" />
                </SegmentTimeline>
            </SegmentTemplate>
        </Representation>
    </AdaptationSet>
</Period>

以下是我用来生成多比特率视频的 ffmpeg 命令

ffmpeg -y -i sample_video.mp4 ^
-c:v libx264 -x264opts "keyint=24:min-keyint=24:no-scenecut" -r 24 ^
-c:a aac -b:a 128k ^
-bf 1 -b_strategy 0 -sc_threshold 0 -pix_fmt yuv420p ^
-map 0:v:0 -map 0:a:0 -map 0:v:0 -map 0:a:0 -map 0:v:0 -map 0:a:0 ^
-b:v:0 250k  -filter:v:0 "scale=-2:240" -profile:v:0 baseline ^
-b:v:1 750k  -filter:v:1 "scale=-2:480" -profile:v:1 main ^
-b:v:2 1500k -filter:v:2 "scale=-2:720" -profile:v:2 high ^
sample_dash.mp4

并生成 mpd 文件

ffmpeg -y -re -i sample_dash.mp4 ^
-map 0 ^
-use_timeline 1 -use_template 1 -window_size 5 -adaptation_sets 
"id=0,streams=v id=1,streams=a" ^
-f dash sample_video.mpd

我还在 chrome 网络选项卡中看到只调用了 init-stream4.m4s 和 init-stream5.m4s。

标签: ffmpegstreamingmpeg-dash

解决方案


你准备好使用 ffmpeg+mp4box dash 打包器了吗?我发现 mp4box 产生更好的片段+清单。

ffmpeg -i tos.mp4 -threads 4 -preset fast -c:v libx264 -profile:v main -level 4.0 -s:v 640x360 -b:v 512k -pix_fmt yuv420p -refs 3 -bf 3 -g 75 -keyint_min 25 -b_strategy 1 -flags +cgop -sc_threshold 0 -movflags negative_cts_offsets+faststart -an -y temp-v1.mp4
ffmpeg -i tos.mp4 -threads 4 -preset fast -c:v libx264 -profile:v main -level 4.0 -s:v 1280x720 -b:v 1500k -pix_fmt yuv420p -refs 3 -bf 3 -g 75 -keyint_min 25 -b_strategy 1 -flags +cgop -sc_threshold 0 -movflags negative_cts_offsets+faststart -an -y temp-v2.mp4
ffmpeg -i tos.mp4 -threads 4 -preset fast -c:v libx264 -profile:v main -level 4.0 -s:v 1920x1080 -b:v 2100k -pix_fmt yuv420p -refs 3 -bf 3 -g 75 -keyint_min 25 -b_strategy 1 -flags +cgop -sc_threshold 0 -movflags negative_cts_offsets+faststart -an -y temp-v3.mp4
ffmpeg -i tos.mp4 -threads 4 -c:a aac -strict experimental -b:a 128k -maxrate:a 128k -bufsize:a 128k -af aresample=44100 -ar 44100 -ac 2 -vn -y temp-a1.mp4
MP4Box -dash 264600 -frag 264600 -dash-scale 44100 -bound -mem-frags -rap -profile dashavc264:live -min-buffer 12000 -bs-switching no -sample-groups-traf -single-traf -subsegs-per-sidx -1 -segment-name $RepresentationID$/$Number$$Init=i$  -out manifest.mpd temp-v1.mp4#trackID=1:id=v1:period=p0 temp-v2.mp4#trackID=1:id=v2:period=p0 temp-v3.mp4#trackID=1:id=v3:period=p0 temp-a1.mp4#trackID=1:id=a1:period=p0

我总是在破折号和公式中使用 audiorate(44100,48000)scale*seconds=value of dash/frag来给出分段文件的持续时间。


推荐阅读