Skip to content

Instantly share code, notes, and snippets.

@sonkm3
Last active September 5, 2021 07:41
Show Gist options
  • Save sonkm3/2d757d6a6859866688a23dfc7e6e9584 to your computer and use it in GitHub Desktop.
Save sonkm3/2d757d6a6859866688a23dfc7e6e9584 to your computer and use it in GitHub Desktop.
ffmpegでmpegdash配信するコマンドライン

第29回Raspberry Piもくもく回

Raspberry Piを使って映像と音声の配信をしてみる(ソフトウェア的にはできるだけ高品質に送りたい)

  • ステレオの音声を送信できるようにする
    • HLS or MpegDASH
  • FFmpegをつかう
    • もしくはGStreamer
  • MpegDASHをつかう
    • もしくはHLS
  • USB接続のwebカメラをつかう
    • もしくはUSB接続のオーディオインタフェース

今回はMpegDASHとFFmpegの組み合わせとUSB接続のwebカメラ(ステレオマイク付き)

詰まっていた点

動画の場合のみ音声映像とも不定期に途切れていた

html内のjsのパラメーターを調整することで途切れないようになった

                'lowLatencyEnabled': false,
                'liveDelay': 5,

音声のみの場合は上の調整はしなくても途切れず再生できる

# 今後の作業

配信用のRaspberry PiのSDカードのイメージを決め打ちで作ってしまうかアプリケーションとして動かす形にするかを決める

前者はFFmpeg+tmpfs+nginx
後者はFFmpeg+tmpfs+python

後者の方法だと権限周りの扱いをどうするか迷う

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div class="page">
<div class="wrapper">
<h1> Raspberry Pi</h1>
<audio data-dashjs-player autoplay controls type="application/dash+xml"></audio>
</div>
</div>
<script src="https://cdn.dashjs.org/latest/dash.all.min.js"></script>
<script>
var player = init();
function init() {
var audio = document.querySelector("audio");
player = dashjs.MediaPlayer().create();
player.initialize(audio, "/mpegdash/output.mpd", true);
player.updateSettings({
'streaming': {
'lowLatencyEnabled': true,
'liveDelay': 1,
'liveCatchUpMinDrift': 0.05,
'liveCatchUpPlaybackRate': 0.5
}
});
return player;
}
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Raspberry Pi Camera</title>
<style>
html body .page {
height: 100%;
width: 100%;
}
video {
width: 800px;
}
.wrapper {
width: 800px;
margin: auto;
}
</style>
</head>
<body>
<div class="page">
<div class="wrapper">
<h1> Raspberry Pi Camera</h1>
<video data-dashjs-player autoplay controls type="application/dash+xml"></video>
</div>
</div>
<script src="https://cdn.dashjs.org/latest/dash.all.min.js"></script>
<script>
var player = init();
function init() {
var video = document.querySelector("video");
player = dashjs.MediaPlayer().create();
player.initialize(video, "mpegdash/output.mpd", true);
player.updateSettings({
'streaming': {
'lowLatencyEnabled': false,
'liveDelay': 5,
'liveCatchUpMinDrift': 0.05,
'liveCatchUpPlaybackRate': 0.5
}
});
return player;
}
</script>
</body>
</html>
ffmpeg \
-y \
-f alsa -thread_queue_size 16384 -i hw:1,0 \
-c:a aac -b:a 256k -ar 44100 -bufsize 256k \
-vn \
-f dash \
-remove_at_exit 1 \
-seg_duration 0.5 \
-window_size 100 \
-streaming 1\
-dash_segment_type mp4 \
output.mpd
ffmpeg \
-y \
-f alsa -thread_queue_size 16384 -r 30 -re -i hw:1,0\
-f v4l2 -thread_queue_size 16384 -s 640x480 -i /dev/video0 \
-c:a aac -b:a 256k -ar 44100 -bufsize 256k \
-c:v h264_omx -b:v 500k -bufsize 1000k -r 30 -vsync cfr -g 300 -profile:v:1 main\
-bsf:v h264_mp4toannexb \
-flags +cgop+loop+global_header \
-ignore_unknown \
-adaptation_sets "id=0,streams=v id=1,streams=a" \
-f dash \
-remove_at_exit 1 \
-seg_duration 1 \
-window_size 100 \
-streaming 1\
-movflags +faststart\
-dash_segment_type mp4 \
output.mpd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment