How to encode to Mp4 file with custom output stream on raspberry pi

Viewed 228

I want to pass my customOutput class to raspberry pi camera function and want to enncode that output to mp4 and save it.

class MyOutput(object):
    def __init__(self):
        self.size = 0

    def write(self, s):
        #get h264 encoded frames?
        #decode and encode them to mp4?
        #append to the file?
        self.size += len(s)

    def flush(self):
        print('%d bytes would have been written' % self.size)

class RecordVideo:
    def __init__(self):
        self.camera = PiCamera()

    def startRecording(self, filename, source, recordingTime, format='h264'):
        self.camera.start_recording(MyOutput(), format)
        self.camera.wait_recording(recordingTime)
        self.camera.stop_recording()

if __name__ == "__main__":
    aat = RecordVideo()
    for i in range(10):
        aat.startRecording('testvideo_'+str(i)+'.h264', './', 10, 'h264')

    aat.stopRecording()

Since piCamera plugin doesnt support mp4 encoding out of the box is there a way i can use any plugin and accomplish creating 10 mp4 files.

Any help will be appreciated.

Thanks

1 Answers

I was trying to do the same encoding task before but not on raspberry pi and I found that ffmpeg is the best for this kind of job. I found that this question is similar to what you are looking for and I know that ffmpeg has python library and it's quite easy to use maybe you can take a look at it.

Related