1

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

Mahmoud Hefny
  • 281
  • 3
  • 13
Tejas Ghutukade
  • 49
  • 2
  • 11

1 Answers1

0

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.

Mahmoud Hefny
  • 281
  • 3
  • 13
  • I did come across ffmpeg and its python library. what i found there is its straight forward to convert an existing h264 file to mp4 with its input and output methods. what i am looking for is where it takes in a stream of frames and converts it and append (assuming thats what we need to do) to a mp4 file. – Tejas Ghutukade Mar 19 '22 at 22:03
  • I have no idea how to accomplish that but if you still didn't reach for a solution you can just save it to h264 file then convert that file to Mp4 format afterwards. – Mahmoud Hefny Mar 21 '22 at 08:55
  • I'm trying to make a dashcam out of a raspberry pi where I need to be able to view the footage by taking the sd card out and plugging it into computer and play its for an older person so they wont know how to convert. I want to make is simple for him. I'm using ffmpeg to convert but the conversion takes longer than the length of the video. so thats not gonna work too. – Tejas Ghutukade Mar 22 '22 at 16:44