MPEG-TS Encoding

adismsc picture adismsc · Nov 25, 2011 · Viewed 17.2k times · Source

I have a file that i need to convert to MPEG-TS so that it fits the below specification:

Elementary stream bitrate [kbit/s] video: 2575 audio: 2 x 192 subtitle: - PAT/PMT: - Stuffing: -

Component TS bitrate [kbit/s] video: 2652 audio: 395 subtitle: 45 PAT/PMT: 45 Stuffing: 62 Total: 3200 CBR

Additional required components: PAT PMT Null packets

Components that might pop up: NIT, SDT, EIT, etc.

vcodec="h264"
acodec="mpga"
bitrate="2500"
arate="192"
samplerate=48000
ext="mpg"
mux="ts"
vlc="/usr/bin/vlc"
fmt="mpg"
dst="/home/adam/test/"

for a in *$fmt; do
$vlc -I dummy -vvv "/home/adam/test/" --sout "#transcode{vcodec=$vcodec,venc=x264{profile=main,level=3.0,hrd=cbr,bframes=2},vb=$bitrate,acodec=$acodec,ab=$arate,samplerate=$samplerate,channels=2}:standard{mux=$mux,dst=\"$dst$a.$ext\",ac$
done

After encoding with the above script everything seems to be ok (for both video and audio bitrate codec is constant) apart from two things: Bitrate of the container should also remain CBR but this is not the case. Also, stuffing component (0x1 ffff) - null packet is missing. Is it possible for you to correct the script to make null packet as well as bitrate of the container constant (3,2 Mbps CBR) ?

The second option is encoding with ffmpeg:

ffmpeg -i video_input.mpg -i audio_input.mp2 -acodec copy -tune zerolatency -x264opts bitrate=2600:vbv-maxrate=2600:vbv-bufsize=166:nal_hrd=cbr -vpre libx264-iptv -vcodec libx264 -f mpegts -muxrate 3200K -y output.ts

but how to unset/disable/remove SDT table?

Answer

Dipan Mehta picture Dipan Mehta · Nov 27, 2011

Given that you are aware of the concept of NULL packets, you might have been working with commercial grade software or hardware in this area.

There is a difference between CBR (of video) and system rate (or multiplexer rate). When video is encoded as CBR, say at 3.2 Mbps, it is quite ok that it fluctuate by a few hundred kbps around that margin. So peak bitrate, could be say 3.3 Mbps. This is quite ok. Adding another 100 kbps of Audio, the total maximum bitrate can be 3.4. Usually, one would set the System rate above 3.6 Mbps or more in that case; where balance are NULL packets.

The system rate 3.5 Mbps CANNOT fluctuate at all. If it does, the PCR based synchronization wont work and basically, things wont work in live environment. So basically, you can think of 3.5 Mbps (about 240 packets in 100 milisecond) can be thought of as a BUS. every seat must be filled up to ensure that transportation is continuous. Usually, it is done such a way that few seats will remain empty.

The sad part is, neither VLC nor any other tool in open source will do it for you.

The hack we used to use was, that we used to send such VLC produced stream over IP (where sending TS stream without NULL packets is ok - and receive that via the output from a Muxer with ASI or such interface, which would have added muxer.

Alternatively you can use Manzanita muxer to convert your Non-null TS stream to proper TS stream.

EDIT:
Based on the comment - if all you need to do is to disable SDT - there are two things needs to be done.

  1. Remove all packets from the PID that corresponds to SDT table. If you are not fully demuxing and re-muxing - a quick way to do that could be to re-stamp the 13 bit PID number by a NULL packet PID number.

  2. Remove the reference of SDT PID value in PAT table. This essentially means that you produce a 3 to 4 packets which is corrected PAT; and replace all PAT packet sequences in the stream by these corrected packets.