def main(output_filename): camera = mo.MMALCamera() preview = mo.MMALRenderer() encoder = mo.MMALVideoEncoder() clock = ClockSplitter() target = mo.MMALPythonTarget(output_filename) # Configure camera output 0 camera.outputs[0].framesize = (640, 480) camera.outputs[0].framerate = 24 camera.outputs[0].commit() # Configure H.264 encoder encoder.outputs[0].format = mmal.MMAL_ENCODING_H264 encoder.outputs[0].bitrate = 2000000 encoder.outputs[0].commit() p = encoder.outputs[0].params[mmal.MMAL_PARAMETER_PROFILE] p.profile[0].profile = mmal.MMAL_VIDEO_PROFILE_H264_HIGH p.profile[0].level = mmal.MMAL_VIDEO_LEVEL_H264_41 encoder.outputs[0].params[mmal.MMAL_PARAMETER_PROFILE] = p encoder.outputs[0].params[ mmal.MMAL_PARAMETER_VIDEO_ENCODE_INLINE_HEADER] = True encoder.outputs[0].params[mmal.MMAL_PARAMETER_INTRAPERIOD] = 30 encoder.outputs[0].params[ mmal.MMAL_PARAMETER_VIDEO_ENCODE_INITIAL_QUANT] = 22 encoder.outputs[0].params[mmal.MMAL_PARAMETER_VIDEO_ENCODE_MAX_QUANT] = 22 encoder.outputs[0].params[mmal.MMAL_PARAMETER_VIDEO_ENCODE_MIN_QUANT] = 22 # Connect everything up and enable everything (no need to enable capture on # camera port 0) clock.inputs[0].connect(camera.outputs[0]) preview.inputs[0].connect(clock.outputs[0]) encoder.inputs[0].connect(clock.outputs[1]) target.inputs[0].connect(encoder.outputs[0]) target.connection.enable() encoder.connection.enable() preview.connection.enable() clock.connection.enable() target.enable() encoder.enable() preview.enable() clock.enable() try: sleep(10) finally: # Disable everything and tear down the pipeline target.disable() encoder.disable() preview.disable() clock.disable() target.inputs[0].disconnect() encoder.inputs[0].disconnect() preview.inputs[0].disconnect() clock.inputs[0].disconnect()
def main(output_filename): camera = mo.MMALCamera() preview = mo.MMALRenderer() encoder = mo.MMALVideoEncoder() clock = ClockSplitter() # Configure camera output 0 camera.outputs[0].framesize = (640, 480) camera.outputs[0].framerate = 24 camera.outputs[0].commit() # Configure H.264 encoder encoder.outputs[0].format = mmal.MMAL_ENCODING_H264 encoder.outputs[0].bitrate = 2000000 encoder.outputs[0].commit() p = encoder.outputs[0].params[mmal.MMAL_PARAMETER_PROFILE] p.profile[0].profile = mmal.MMAL_VIDEO_PROFILE_H264_HIGH p.profile[0].level = mmal.MMAL_VIDEO_LEVEL_H264_41 encoder.outputs[0].params[mmal.MMAL_PARAMETER_PROFILE] = p encoder.outputs[0].params[mmal.MMAL_PARAMETER_VIDEO_ENCODE_INLINE_HEADER] = True encoder.outputs[0].params[mmal.MMAL_PARAMETER_INTRAPERIOD] = 30 encoder.outputs[0].params[mmal.MMAL_PARAMETER_VIDEO_ENCODE_INITIAL_QUANT] = 22 encoder.outputs[0].params[mmal.MMAL_PARAMETER_VIDEO_ENCODE_MAX_QUANT] = 22 encoder.outputs[0].params[mmal.MMAL_PARAMETER_VIDEO_ENCODE_MIN_QUANT] = 22 output = io.open(output_filename, 'wb') def output_callback(port, buf): output.write(buf.data) return bool(buf.flags & mmal.MMAL_BUFFER_HEADER_FLAG_EOS) # Connect everything up (no need to enable capture on camera port 0) clock.connect(camera.outputs[0]) preview.connect(clock.outputs[0]) encoder.connect(clock.outputs[1]) encoder.outputs[0].enable(output_callback) try: sleep(10) finally: preview.disconnect() encoder.disconnect() clock.disconnect()
def __init__(self): self.__resolutions = { # 'short': [fps, (width, height)] '720p': [30, (1280, 720)], '1080p': [24, (1920, 1080)], } self.__config = ConfigParser() self.__config.read("/etc/raspberrydashcam/config.ini") # Set up the basic stuff self._title = self.__config["dashcam"]["title"] self._fps, self._resolution = self.__resolutions[ self.__config["camera"]["resolution"]] self._bitrate = int(self.__config["camera"]["bitrate"]) # Create the filename that we're going to record to. self._filename = datetime.now().strftime( "/mnt/storage/%Y-%m-%d-%H-%M-%S.mp4") # This is just a place holder. self.__fmpeg_instance = None # This is the ffmpeg command we will run self.__ffmpeg_command = [ "/usr/bin/ffmpeg", #"-loglevel quiet -stats", "-async", "1", "-vsync", "1", "-ar", "44100", "-ac", "1", "-f", "s16le", "-f", "alsa", "-thread_queue_size", "10240", "-i", "hw:2,0", "-f", "h264", "-probesize", "10M", "-r", str(self._fps), "-thread_queue_size", "10240", "-i", "-", # Read from stdin "-crf", "22", "-vcodec", "copy", "-acodec", "aac", "-ab", "128k", "-g", str(self._fps * 2), # GOP should be double the fps. "-r", str(self._fps), "-f", "mp4", self._filename ] self.__camera = mo.MMALCamera() self.__preview = mo.MMALRenderer() self.__encoder = mo.MMALVideoEncoder() self.__DashCamData = DashCamData(title=self._title, resolution=self._resolution) # Here we start the ffmpeg process and at the same time # open up a stdin pipe to it. self.__ffmpeg_instance = subprocess.Popen(" ".join( self.__ffmpeg_command), shell=True, stdin=subprocess.PIPE) # Here we specify that the script should write to stdin of the # ffmpeg process. self.__target = mo.MMALPythonTarget(self.__ffmpeg_instance.stdin) # Setup resolution and fps self.__camera.outputs[0].framesize = self._resolution self.__camera.outputs[0].framerate = self._fps # Commit the two previous changes self.__camera.outputs[0].commit() # Do base configuration of encoder. self.__encoder.outputs[0].format = mmal.MMAL_ENCODING_H264 self.__encoder.outputs[0].bitrate = self._bitrate # Commit the encoder changes. self.__encoder.outputs[0].commit() # Get current MMAL_PARAMETER_PROFILE from encoder profile = self.__encoder.outputs[0].params[mmal.MMAL_PARAMETER_PROFILE] # Modify the proflle # Set the profile to MMAL_VIDEO_PROFILE_H264_HIGH profile.profile[0].profile = mmal.MMAL_VIDEO_PROFILE_H264_HIGH profile.profile[0].level = mmal.MMAL_VIDEO_LEVEL_H264_41 # Now make sure encoder get's the modified profile self.__encoder.outputs[0].params[mmal.MMAL_PARAMETER_PROFILE] = profile # Now to stuff that is completely stolen # which I do not yet know what they do. self.__encoder.outputs[0].params[ mmal.MMAL_PARAMETER_VIDEO_ENCODE_INLINE_HEADER] = True self.__encoder.outputs[0].params[mmal.MMAL_PARAMETER_INTRAPERIOD] = 48 self.__encoder.outputs[0].params[ mmal.MMAL_PARAMETER_VIDEO_ENCODE_INITIAL_QUANT] = 17 self.__encoder.outputs[0].params[ mmal.MMAL_PARAMETER_VIDEO_ENCODE_MAX_QUANT] = 17 self.__encoder.outputs[0].params[ mmal.MMAL_PARAMETER_VIDEO_ENCODE_MIN_QUANT] = 17 # Stolen from picamera module, very clever stuff self.__mirror_parameter = { (False, False): mmal.MMAL_PARAM_MIRROR_NONE, (True, False): mmal.MMAL_PARAM_MIRROR_VERTICAL, (False, True): mmal.MMAL_PARAM_MIRROR_HORIZONTAL, (True, True): mmal.MMAL_PARAM_MIRROR_BOTH, } # Set initial values self.vflip = self.hflip = False # Get actual config values from config if self.__config["camera"].getboolean("vflip"): self.set_vflip(True) if self.__config["camera"].getboolean("hflip"): self.set_hflip(True) self.__camera.control.params[ mmal.MMAL_PARAMETER_VIDEO_STABILISATION] = True mp = self.__camera.control.params[mmal.MMAL_PARAMETER_EXPOSURE_MODE] print(mp) mp.value = mmal.MMAL_PARAM_EXPOSUREMODE_AUTO self.__camera.control.params[mmal.MMAL_PARAMETER_EXPOSURE_MODE] = mp mp = self.__camera.control.params[mmal.MMAL_PARAMETER_AWB_MODE] mp.value = mmal.MMAL_PARAM_AWBMODE_HORIZON self.__camera.control.params[mmal.MMAL_PARAMETER_AWB_MODE] = mp
from signal import pause # define values from the camera window #my desktop 1600x900 (800x450) camResW = 200 camResH = 225 camFramR = 30 # starting corner of your windows (upper-left position) cornerX = 500 cornerY = 600 windowSpacer = 10 # space between the 2 windows camera = mo.MMALCamera() splitter = mo.MMALSplitter() render_l = mo.MMALRenderer() render_r = mo.MMALRenderer() # The 960 and 720 are the resolution. This can be changed to fill your display properly. I believe I'm using 960 and 1080 on mine. camera.outputs[0].framesize = (camResW, camResH) # This originally was 30, but then I the latency is too high and it's nearly impossible to navigate while wearing a head mounted display. camera.outputs[0].framerate = camFramR camera.outputs[0].commit() p = render_l.inputs[0].params[mmal.MMAL_PARAMETER_DISPLAYREGION] p.set = mmal.MMAL_DISPLAY_SET_FULLSCREEN | mmal.MMAL_DISPLAY_SET_DEST_RECT p.fullscreen = False # These parameters control the X,Y and scale of the left image showing up on your display p.dest_rect = mmal.MMAL_RECT_T(cornerX, cornerY, camResW, camResH) render_l.inputs[0].params[mmal.MMAL_PARAMETER_DISPLAYREGION] = p # These control the X,Y and the scale of the right image showing up on your display. p.dest_rect = mmal.MMAL_RECT_T(cornerX + windowSpacer + camResW, cornerY,
from picamera import mmal, mmalobj as mo source = mo.MMALPythonSource('test.h264') decoder = mo.MMALVideoDecoder() preview = mo.MMALRenderer() # read the input as h264 source.outputs[0].format = mmal.MMAL_ENCODING_H264 source.outputs[0].framerate = 25 source.outputs[0].framesize = (1280, 720) source.outputs[0].commit() # decoder input port decoder.inputs[0].copy_from(source.outputs[0]) decoder.inputs[0].commit() # decoder output port decoder.outputs[0].copy_from(decoder.inputs[0]) decoder.outputs[0].format = mmal.MMAL_ENCODING_I420 decoder.outputs[0].commit() # connect source -> decoder -> preview decoder.inputs[0].connect(source.outputs[0]) decoder.inputs[0].connection.enable() preview.inputs[0].connect(decoder.outputs[0]) # enable everything preview.enable() decoder.enable() source.enable()