Skip to content

Instantly share code, notes, and snippets.

@orumin
Last active February 10, 2023 09:25
Show Gist options
  • Save orumin/785dd5da8c29c5b24754 to your computer and use it in GitHub Desktop.
Save orumin/785dd5da8c29c5b24754 to your computer and use it in GitHub Desktop.
MPEG2-TS to H.264 within gstreamer on RaspberryPi
#!/usr/bin/bash
#
# Usage: mpegts2h264.sh <src.ts> <dst.mp4>
#
program_no=$(ffmpeg -i $1 2>&1 | grep Program | head -n 1 | awk '{ print $2 }')
video_sid=$(ffmpeg -i $1 2>&1 | grep Stream | grep Video | sed -e "s/^.*Stream #0:0\[0x\([0-9a-f][0-9a-f][0-9a-f]\)\].*$/\1/g")
audio_sid=$(ffmpeg -i $1 2>&1 | grep Stream | grep Audio | sed -e "s/^.*Stream #0:1\[0x\([0-9a-f][0-9a-f][0-9a-f]\)\].*$/\1/g")
gst-launch-1.0 filesrc location=./$1 ! progressreport ! \
tsdemux program-number=$program_no name=demuxer \
demuxer.video_0$video_sid ! video/mpeg ! queue max-size-time=0 max-size-buffers=0 ! \
mpegvideoparse ! omxmpeg2videodec ! \
videoconvert ! omxh264enc target-bitrate=4500000 control-rate=variable periodicty-idr=240 ! \
video/x-h264,stream-format=byte-stream,profile=high ! h264parse ! mux. \
demuxer.audio_0$audio_sid ! audio/mpeg ! queue max-size-time=0 max-size-buffers=0 ! \
aacparse ! avdec_aac ! audioresample ! audioconvert dithering=0 ! faac bitrate=128000 ! \
mp4mux streamable=true name=mux ! filesink location=$2
@orumin
Copy link
Author

orumin commented Dec 1, 2014

progressreport0が100%になったところで,次のエラーが報告される.
ERROR: from element /GstPipeline:pipeline0/GstTSDemux:demuxer: Internal data stream error.
Additional debug info:
mpegtsbase.c(1354): mpegts_base_loop (): /GstPipeline:pipeline0/GstTSDemux:demuxer:
No program activated before EOS
ERROR: pipeline doesn't want to preroll.

@orumin
Copy link
Author

orumin commented Dec 1, 2014

tsdemuxに指定しているprogram-number,これを指定しなければprogressが始まる前に
omxmpeg2videodecのInternal stream errorで落ちる.
値はたまたま見つけた2chのLinuxで録画してる人達のスレの書き込みの1つを参考にしたが,
何のパラメータかわかっていない.要調査

@orumin
Copy link
Author

orumin commented Dec 24, 2014

ffmpeg -i でProgram NumberとStreamIDが指定可能
Program Numberを正しく指定したところ、上記同様にprogressが始まる前にエラー。
これは結局、入力ソースが1920x1080だったのにエンコード時に1280x720にしたのが原因らしい。
videoscale ! videoconvert !をomxh264encの前に挿入して解決。

@orumin
Copy link
Author

orumin commented Dec 24, 2014

もしかしたら、StreamIDのほうは指定せずdemux.videoとかにするだけで勝手にしてくれるかも。
後で確認します。

@orumin
Copy link
Author

orumin commented Dec 24, 2014

StreamIDも指定しないとエラー出た

@orumin
Copy link
Author

orumin commented Dec 24, 2014

リサイズ処理を入れるとバカみたいに重くなるのを確認。逆に、リサイズさえしなければ結構速い

@orumin
Copy link
Author

orumin commented Dec 25, 2014

自動でProgram numberとStream IDを指定するようにしてみた。あとリサイズ処理はオミット

@xswqaz
Copy link

xswqaz commented Oct 22, 2015

Menu IDがあれば、勝手にやってくれます。(SID指定しなくても)

@orumin
Copy link
Author

orumin commented Jun 20, 2016

これやったの大分前なのでMenu IDがあったか覚えてないです.
たぶん録画データに1SEG用のも入ってたりして辛いしなんとかしようとおもってSID指定した気がするのできっとわざとです.
覚えてないですが

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment