Skip to content

Commit 2c84fc0

Browse files
committed
Make input codec configurable
1 parent 5c0edab commit 2c84fc0

1 file changed

Lines changed: 81 additions & 48 deletions

File tree

src/ac_training_lab/picam/device.py

Lines changed: 81 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ def start_stream(ffmpeg_url, stream_key):
150150
video_maxrate = os.environ.get("PICAM_VIDEO_MAXRATE", video_bitrate)
151151
video_bufsize = os.environ.get("PICAM_VIDEO_BUFSIZE", "2000k")
152152
x264_preset = os.environ.get("PICAM_X264_PRESET", "ultrafast")
153+
input_codec = os.environ.get("PICAM_INPUT_CODEC", "raw").lower()
154+
camera_bitrate = os.environ.get("PICAM_CAMERA_BITRATE", "1200000")
153155
gop = str(stream_fps * 2)
154156

155157
libcamera_cmd = [
@@ -166,9 +168,22 @@ def start_stream(ffmpeg_url, stream_key):
166168
str(stream_height),
167169
"--framerate",
168170
str(stream_fps),
169-
"--codec",
170-
"yuv420",
171171
]
172+
if input_codec == "h264":
173+
libcamera_cmd.extend(
174+
[
175+
"--codec",
176+
"h264",
177+
"--profile",
178+
"baseline",
179+
"--intra",
180+
gop,
181+
"--bitrate",
182+
str(camera_bitrate),
183+
]
184+
)
185+
else:
186+
libcamera_cmd.extend(["--codec", "yuv420"])
172187

173188
if CAMERA_VFLIP:
174189
libcamera_cmd.extend(["--vflip"])
@@ -177,52 +192,70 @@ def start_stream(ffmpeg_url, stream_key):
177192

178193
libcamera_cmd.extend(["-o", "-"])
179194

180-
ffmpeg_cmd = [
181-
"ffmpeg",
182-
"-thread_queue_size",
183-
"1024",
184-
"-use_wallclock_as_timestamps",
185-
"1",
186-
"-fflags",
187-
"+genpts",
188-
"-analyzeduration",
189-
"10000000",
190-
"-probesize",
191-
"10000000",
192-
"-f",
193-
"rawvideo",
194-
"-pix_fmt",
195-
"yuv420p",
196-
"-s",
197-
f"{stream_width}x{stream_height}",
198-
"-r",
199-
str(stream_fps),
200-
"-i",
201-
"pipe:0",
202-
"-c:v",
203-
"libx264",
204-
"-preset",
205-
x264_preset,
206-
"-tune",
207-
"zerolatency",
208-
"-pix_fmt",
209-
"yuv420p",
210-
"-g",
211-
gop,
212-
"-keyint_min",
213-
gop,
214-
"-sc_threshold",
215-
"0",
216-
"-b:v",
217-
video_bitrate,
218-
"-maxrate",
219-
video_maxrate,
220-
"-bufsize",
221-
video_bufsize,
222-
"-f",
223-
"flv",
224-
f"{ffmpeg_url.rstrip('/')}/{stream_key}",
225-
]
195+
if input_codec == "h264":
196+
input_opts = [
197+
"-f",
198+
"h264",
199+
"-r",
200+
str(stream_fps),
201+
"-i",
202+
"pipe:0",
203+
]
204+
else:
205+
input_opts = [
206+
"-f",
207+
"rawvideo",
208+
"-pix_fmt",
209+
"yuv420p",
210+
"-s",
211+
f"{stream_width}x{stream_height}",
212+
"-r",
213+
str(stream_fps),
214+
"-i",
215+
"pipe:0",
216+
]
217+
218+
ffmpeg_cmd = (
219+
[
220+
"ffmpeg",
221+
"-thread_queue_size",
222+
"1024",
223+
"-use_wallclock_as_timestamps",
224+
"1",
225+
"-fflags",
226+
"+genpts",
227+
"-analyzeduration",
228+
"10000000",
229+
"-probesize",
230+
"10000000",
231+
]
232+
+ input_opts
233+
+ [
234+
"-c:v",
235+
"libx264",
236+
"-preset",
237+
x264_preset,
238+
"-tune",
239+
"zerolatency",
240+
"-pix_fmt",
241+
"yuv420p",
242+
"-g",
243+
gop,
244+
"-keyint_min",
245+
gop,
246+
"-sc_threshold",
247+
"0",
248+
"-b:v",
249+
video_bitrate,
250+
"-maxrate",
251+
video_maxrate,
252+
"-bufsize",
253+
video_bufsize,
254+
"-f",
255+
"flv",
256+
f"{ffmpeg_url.rstrip('/')}/{stream_key}",
257+
]
258+
)
226259

227260
p1 = subprocess.Popen(
228261
libcamera_cmd, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL

0 commit comments

Comments
 (0)