Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create video from camera frames

I've been playing around with v4l2 and I finally managed to connect to my laptop's camera and set it to stream.

At the moment I save the frames as 1.jpg, 2.jpg etc.

Thinking on a basic level, I need a storage container for those jpegs and then a video player runs the container contents in sequence and I get video.

I assume the video format is going to be my container.

How do I create and write to one?

like image 253
user3017869 Avatar asked Sep 11 '25 20:09

user3017869


1 Answers

The easiest would be to save your JPEG images in a video file of format MJPEG, which is a simple video format consisting a series of JPEG images.

You may turn to different ready-to-use encoders to turn a series of JPEG images into an MJPEG (or any other format) video file, such as ffmpeg. Using ffmpeg, you could do it with the following command:

ffmpeg -r 2 -i "%02d.jpg" -vcodec mjpeg test.avi

If you want to do it in Go, you may use the dead-simple github.com/icza/mjpeg package (disclosure: I'm the author).

Let's see an example how to turn the JPEG files 1.jpg, 2.jpg, ..., 10.jpg into a movie file:

checkErr := func(err error) {
    if err != nil {
        panic(err)
    }
}

// Video size: 200x100 pixels, FPS: 2
aw, err := mjpeg.New("test.avi", 200, 100, 2)
checkErr(err)

// Create a movie from images: 1.jpg, 2.jpg, ..., 10.jpg
for i := 1; i <= 10; i++ {
    data, err := ioutil.ReadFile(fmt.Sprintf("%d.jpg", i))
    checkErr(err)
    checkErr(aw.AddFrame(data))
}

checkErr(aw.Close())
like image 60
icza Avatar answered Sep 13 '25 09:09

icza