I'm using a pi zero and the v2.1 camera to record a series of video clips over a certain duration with pauses in between. The issue is that at the beginning of each clip there's a noticeable increase in compression artifacts. Is there a way to get around this problem? Besides chopping off the first quarter second of video? Thanks! My code:

from picamera import PiCamera
import time
import os
import datetime

clip_dict = {
            'duration' : 240,
            'clip_duration' : 2,
            'warmup_duration' : 3,
            'pause_duration' : 5,
            'resolution' : (1920, 1080),
            'dst_dir' : '/home/pi/Documents/clips/'

def record_clips(clip_dict):
    clip_num = 0
    end_point = clip_dict['duration'] + time.time()
    while(time.time() < end_point):
            with PiCamera() as camera:
                    camera.resolution = clip_dict['resolution']
                    camera.start_recording('{0}{1}.h264'.format(clip_dict['dst_dir'], str(clip_num).zfill(4)))
            clip_num = clip_num + 1

def main():
    ts = time.time()
    stamp = datetime.datetime.fromtimestamp(ts).strftime("%Y-%m-%d-%H-%M-%S")
    if os.path.exists(clip_dict['dst_dir']):
        dst_dir = clip_dict['dst_dir'] + stamp + '/' 
        if not os.path.exists(dst_dir):
            raise IOError

    clip_dict['dst_dir'] = dst_dir

if __name__ == '__main__':

I'm then converting the .h264 to .mkv like so:

for f in ./*.h264; do ffmpeg -r 30 -i $f -vcodec copy $f.mkv; done
  • Have you confirmed that the artifacts are there in both the h264 and mkv files? – goobering Jun 17 '16 at 1:04
  • @goobering yes, I can set vlc demuxer to default to h264, play with video without the mkv (or mp4 or mov) container and the artifacts are still present. – moorej Jun 17 '16 at 5:25
  • 1
    There's maybe a couple of things to poke at. If your scene's particularly busy/noisy the CPU will have to try hard to achieve your compression settings. Experiment with relatively static scenes. It's also plausible that the storage media may be struggling to keep pace with the video data rate. Experiment with lower resolutions (it's worth going to stupidly low for testing) and lower frame rates and, if things improve, evaluate how good your SD card is. There are varying opinions on the value of class 10 cards for the Pi, but they're fairly cheap and it certainly won't hurt. – goobering Jun 17 '16 at 9:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.