在 Matroska 容器中实时转码为 H264+AAC

在 Matroska 容器中实时转码为 H264+AAC

设想

我有一个电影库,希望使用 VLC 将其流式传输到我的 Android 平板电脑进行播放。我想使用 H264+AAC 以获得通过移动互联网连接获得最佳视觉质量。我还希望使用支持文本字幕的 Matroska 容器,而不是 MPEG2TS 支持的位图字幕。

我已经安装了 Mediatomb,并且 UPnP 运行正常,我无需转码就可以顺利地传输视频和音频。

问题

我可以顺利地将代码转码并下采样为 MPEG2。但 H264 播放会在 10-40 秒后停止,每次播放的视频都停在完全相同的位置,但每个视频的播放位置都不同。PC 上的 VLC 甚至无法开始播放(在第一帧后停止)。

配置

我正在使用这个脚本

#!/bin/bash
LINES=720
PRESET=veryfast
PROFILE=main
TUNE=zerolatency

AUDIO="-c:a libfaac -b:a 128k -ar 48000 -ac 2 -async 1"
# Works well
VIDEO="-c:v mpeg2video -b 8192k"
# Freezes after a few seconds seconds.
#VIDEO="-c:v libx264 -preset ${PRESET} -tune ${TUNE} -profile ${PROFILE}" 
SUBTITLES="-c:s copy"

exec /usr/bin/ffmpeg -threads 2 -i "${1}" -filter:v scale=-1:720 $VIDEO \
$AUDIO $SUBTITLES -f matroska -y "${2}" &> /store/tmp/log

我的 mediatomb 配置及其相关部分:

<profile name="h264stream" enabled="yes" type="external">
  <mimetype>video/x-matroska</mimetype>
  <accept-url>no</accept-url>
  <first-resource>yes</first-resource>
  <hide-original-resource>yes</hide-original-resource>
  <accept-ogg-theora>yes</accept-ogg-theora>
  <sample-frequency>48000</sample-frequency>
  <audio-channels>2</audio-channels>
  <agent command="/etc/mediatomb/ffmpeg.sh" arguments="%in %out"/>
  <buffer size="104857600" chunk-size="262144" fill-size="524288"/>
</profile>

如果我这样做,tail -f /store/tmp/log我可以看到,即使平板电脑上的播放停止后,FFMPEG 进程仍在编码。事实上,它非常乐意地咀嚼。而且它的编码速度也比源材料快,所以它不会落后。平板电脑上的播放很流畅,直到突然停止。

我尝试使用不同的预设、配置文件和调整参数但都无济于事,冻结的时间似乎与编码器的运行速度成反比(编码速度快,冻结时间短)

答案1

调整 h264 流以使其可流式传输:

显然,h264 编解码器具有一种特殊模式,该模式是实现高效流式传输所必需的,您可以使用以下方式启用它:-bsf:v h264_mp4toannexb

脚本

我用来设置 H264+AAC matroska 流媒体管道的脚本是这样的:

#!/bin/bash 
# ----------------------------------------------------------------------------
# This script is a helper to transcode a video to H264+AAC with subtitles to a 
# Matroska (.mkv) container that is suitable for live streaming to a mobile 
# device. It will transcode media that is not H264 or that has too high 
# resolution. It will not upsample content. 
# 
# Other suitable containers (and reasons for not using them) include: 
# * ASF (Microsoft, proprietary) 
# * MPEG2 Transport Stream (Standard, only supports bitmap subtitles) 
# * WebM (Has no support for metadata) 
# * DivX (Can't contain H264) 
# * FLV (Proprietary Bad support on target device) 
# * MP4 (Only bitmap subtitles, didn't work for streaming with FFMPEG) 
# * OGG (No support for H264) 
# ----------------------------------------------------------------------------

# ----------------------------------------------------------------------------
# Video options 
# ----------------------------------------------------------------------------
LINES=720

# One of: ultrafast,superfast, veryfast, faster, fast, medium, slow, slower, 
# veryslow or placebo 
PRESET=ultrafast 

# One of: baseline, main, high, high10, high422 or high444 
PROFILE=high10 

# One of: film animation grain stillimage psnr ssim fastdecode zerolatency 
TUNE=zerolatency 

# ----------------------------------------------------------------------------
# Audio options 
# ----------------------------------------------------------------------------
AUDIO="-c:a libfaac -b:a 128k -ar 48000 -ac 2 -async 1"

SUBTITLES="-c:s copy"

# ----------------------------------------------------------------------------
# Read input video parameters 
# ----------------------------------------------------------------------------
IN_RESOLUTION=`/usr/bin/ffmpeg -i "${1}" 2>&1 | grep Video | \
    perl -lane 'print $1 if /(\d+x\d+)/'`
IN_CODEC=`/usr/bin/ffmpeg -i "${1}" 2>&1 | grep Video | \
    perl -lane 'print $1 if /Video: (\S+)/'`
IN_DIMS=(${IN_RESOLUTION//x/ })
V_TRANSCODE="-c:v libx264 -bsf:v h264_mp4toannexb -preset ${PRESET} \
    tune ${TUNE} -profile:v ${PROFILE}"
V_COPY="-c:v copy -bsf:v h264_mp4toannexb"

if [ "${IN_DIMS[1]}" > "${LINES}" ]; then
    SCALE="-filter:v scale=-1:${LINES} ${OPT_TRANSCODE}"
else
    if ["${IN_CODEC}" != "h264" ]; then
        VIDEO=$OPT_TRANSCODE
    else
        VIDEO=$V_COPY
    fi
fi

exec /usr/bin/ffmpeg -threads `nproc` -i "${1}" $VIDEO $AUDIO $SUBTITLES \
    -f matroska -y "${2}" &> /store/tmp/log

去做:

如果找到外部文件,则读取字幕并将其添加到 matroska 流中。如果音频流已经是合适的格式,则不对其进行转码。

相关内容