Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to save pygame camera as video output

I was try this tutorial Stream Webcam Video to PyGame nothing error.. https://www.youtube.com/watch?v=H6ijuSmv5N0

and the script like this..

import pygame, sys
import pygame.camera
from pygame.locals import *

pygame.init()
pygame.camera.init()

screen = pygame.display.set_mode((640, 480))

cam = pygame.camera.Camera("/dev/video0", (640, 480))
cam.start()

while 1:
    image = cam.get_image()
    screen.blit(image, (0,0))
    pygame.display.update()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            #save as video output
            sys.exit()

But.. in here I have a problem when I want to save that Stream as video output.. like a mp4, 3gp, avi and other.

In this thread pygame.image.save(img, "image.jpg") save the output as image. https://stackoverflow.com/a/20502651/3445802

like image 909
agaust Avatar asked Feb 09 '15 13:02

agaust


People also ask

How do I save a pygame window as a video?

You have to make use of another library which to feed image frames, to render the video - or save frame by frame in a folder, numbering the file names in crescent order, and convert the result to a video with an utility later.

Does pygame support video?

Pygame can playback video and audio from basic encoded MPEG-1 video files. Movie playback happens in background threads, which makes playback easy to manage. The audio for Movies must have full control over the sound system. This means the pygame.


1 Answers

Pygame's multimedia output capabilities are severily limited: It can only save uncompressed BMP images, and there is no way it can save a video format.

You have to make use of another library which to feed image frames, to render the video - or save frame by frame in a folder, numbering the file names in crescent order, and convert the result to a video with an utility later.

This project seens to feature a class to call libffmpeg to encode videos, passing frame by frame in a Python call: https://github.com/kanryu/pipeffmpeg - you will just need a way to convert the pygame Surface object to the expected "frameraw" attribute of ffmpeg.

https://github.com/kanryu/pipeffmpeg/blob/master/pipeffmpeg.py

like image 196
jsbueno Avatar answered Oct 02 '22 14:10

jsbueno