0

I use: Psychopy Version: 1.90.3 Python 2.7

I am trying to capture videofiles during my experiment using cv2's VideoWriter(). The VideoWriter() function needs the fourcc codec as the second argument but it seems that the encoding via "cv2.VideoWriter_fourcc(*'XVID')" collides with the unicode_literals. If unicode_literals is loaded, the error message pops up for the fourcc argument:

"TypeError: Expected single character string for argument 'c1'"

Here is my code snippet for the VideoRecording() function I want to call in my experiment source code. The error concerns the definition

fourcc = cv2.VideoWriter_fourcc(*'XVID')
def VideoRecording(video_width,video_height,video_fps,seconds):

    cap = cv2.VideoCapture(0)
    cap.set(3,video_width) # width
    cap.set(4,video_height) #height 
    cap.set(5,video_fps) # frames per second

    # Define the codec and create VideoWriter object
    fps = cap.get(5)
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    filename = str('output_intrasync_video/' + subject[0] + '_' + str(trial_block) + '_' + str(x) + '.avi')
    out = cv2.VideoWriter(filename,fourcc,video_fps, (video_width,video_height))

    start = time.time()
    zeitdauer=0
    while(zeitdauer<seconds):
        end=time.time()
        zeitdauer=end-start
        ret, frame = cap.read()
        if ret==True:
            frame = cv2.flip(frame,180)
            # write the flipped frame
            out.write(frame)

            #cv2.imshow('frame',frame)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
        else:
            break

    # Release everything if job is finished
    cap.release()
    out.release()
    cv2.destroyAllWindows()

The function works fine without loading unicode_literals, but I need to load unicode_literals for the functioning of the rest of my code.

Any ides why the fourcc codec argument is incompatible with unicode_literals?

Cheers, Carrie

nathancy
  • 42,661
  • 14
  • 115
  • 137
carrieOn
  • 1
  • 1
  • 1
    I really don't know, but whenever there are issues that are possibly Unicode-related, your life might be easier if you switch to running under Python 3. There has been a *lot* of development in PsychoPy lately, so it might also be a good time to jump to version 3.1+ of PsychoPy, running under Python 3.6+ – Michael MacAskill Jun 19 '19 at 09:34
  • 1
    Probably because it doesn’t expect Unicode or something. `*b'XVID'` to the rescue – Ry- Jun 19 '19 at 21:38

0 Answers0