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

Emi*_* L. 3 upnp ffmpeg real-time transcode x264

设想

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

我安装了 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
Run Code Online (Sandbox Code Playgroud)

我的 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>
Run Code Online (Sandbox Code Playgroud)

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

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

Emi*_* L. 5

将 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
Run Code Online (Sandbox Code Playgroud)

去做:

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