ffmpegmux.py (1357B)
1 import picamerax as picamera 2 import subprocess 3 import time 4 5 quality = 20 6 bitrate = 8888888 7 profilelevel='4.2' 8 9 # FFmpeg command to take H.264 input from stdin and output to MP4 10 ffmpeg_cmd = [ 11 'ffmpeg', 12 '-i', 'pipe:0', # Input from stdin (raw H.264 from PiCamera) 13 '-c:v', 'copy', # Use hardware encoder 14 '-b:v', str(bitrate), 15 '-level:v', '4.2', 16 '-g', '1', 17 '-f', 'mp4', # Output format 18 'output.mp4', # Output file 19 '-loglevel','debug', 20 '-y' # Overwrite output file if it exists 21 ] 22 23 24 #'-c:v h264_omx -profile:v high -level:v 4.2 -preset slow -bsf:v h264_metadata=level=4.2 -g 1 -b:v '+str(bitrate)+' -c:a copy ' 25 # Start FFmpeg process 26 process = subprocess.Popen(ffmpeg_cmd, stdin=subprocess.PIPE) 27 28 # Initialize PiCamera 29 with picamera.PiCamera() as camera: 30 camera.resolution = (1920, 1080) # High Quality Camera resolution 31 camera.framerate = 24.97 32 camera.start_preview() 33 # Start recording and pipe to FFmpeg 34 print("Starting recording...") 35 camera.start_recording(process.stdin, format='h264', bitrate = 8888888, level=profilelevel, quality=quality, intra_period=1) 36 camera.wait_recording(10) # Record for 10 seconds 37 camera.stop_recording() 38 39 # Close the FFmpeg process 40 process.stdin.close() 41 process.wait() 42 print("Recording complete!")