Echtzeit-Transcodierung in H264 + AAC im Matroska-Container

5042
Emily L.

Szenario

Ich habe eine Filmbibliothek, die ich mit VLC für die Wiedergabe auf mein Android-Tablet streamen kann. Ich möchte H264 + AAC verwenden, um eine bestmögliche visuelle Qualität über eine mobile Internetverbindung zu erzielen. Ich möchte auch einen Matroska-Container verwenden, der Textuntertitel unterstützt, im Gegensatz zu Bitmap-Untertiteln, die MPEG2TS unterstützt.

Ich habe Mediatomb installiert und UPnP funktioniert einwandfrei. Ich kann Video und Audio problemlos streamen, ohne zu transcodieren.

Problem

Ich kann prima umcodieren und in MPEG2 umwandeln. Die Wiedergabe von H264 stoppt jedoch nach 10-40 Sekunden an genau derselben Stelle für jedes Video, jedoch für jedes Video an verschiedenen Stellen. Die Wiedergabe startet nicht einmal bei VLC auf dem PC (stoppt nach dem ersten Bild).

Configs

Ich verwende dieses Skript

#!/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 $ -tune $ -profile $"  SUBTITLES="-c:s copy"  exec /usr/bin/ffmpeg -threads 2 -i "$" -filter:v scale=-1:720 $VIDEO \ $AUDIO $SUBTITLES -f matroska -y "$" &> /store/tmp/log 

Meine Mediatomb-Konfiguration mit relevantem Abschnitt:

<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> 

Wenn ich dies tue, tail -f /store/tmp/logkann ich sehen, dass der FFMPEG-Prozess auch nach dem Stoppen der Wiedergabe auf dem Tablet noch kodiert. Infact, es ist ziemlich glücklich, weg zu kauen. Es ist auch eine Kodierung, die schneller ist als das Ausgangsmaterial, damit sie nicht hinterherhinkt. Die Wiedergabe auf dem Tablet läuft so lange, bis sie plötzlich stoppt.

Ich habe versucht, verschiedene Presets, Profile und Tune-Parameter ohne Erfolg zu verwenden. Die Zeit bis zum Einfrieren scheint umgekehrt proportional zu sein, wie schnell der Encoder läuft (hohe Codiergeschwindigkeit, kurze Einfrierzeit).

2

1 Antwort auf die Frage

4
Emily L.

Stellen Sie den h264-Stream so ein, dass er streamfähig ist:

Anscheinend hat der h264-Codec einen speziellen Modus, der für effizientes Streaming erforderlich ist, und Sie ermöglichen es mit: -bsf:v h264_mp4toannexb

Skript

Das Skript, das ich zum Einrichten einer H264 + AAC-Matroska-Streaming-Pipe verwende, lautet wie folgt:

#!/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 "$" 2>&1 | grep Video | \ perl -lane 'print $1 if /(\d+x\d+)/'` IN_CODEC=`/usr/bin/ffmpeg -i "$" 2>&1 | grep Video | \ perl -lane 'print $1 if /Video: (\S+)/'` IN_DIMS=($) V_TRANSCODE="-c:v libx264 -bsf:v h264_mp4toannexb -preset $ \ tune $ -profile:v $" V_COPY="-c:v copy -bsf:v h264_mp4toannexb"  if [ "$" > "$" ]; then SCALE="-filter:v scale=-1:$ $" else if ["$" != "h264" ]; then VIDEO=$OPT_TRANSCODE else VIDEO=$V_COPY fi fi  exec /usr/bin/ffmpeg -threads `nproc` -i "$" $VIDEO $AUDIO $SUBTITLES \ -f matroska -y "$" &> /store/tmp/log 

MACHEN:

Lassen Sie es Untertitel aus externen Dateien lesen, falls vorhanden, und fügen Sie sie dem Matroska-Stream hinzu. Lassen Sie den Audiostrom nicht transcodieren, wenn er bereits in einem geeigneten Format vorliegt.