2

I am using opencv and successfully created a video file using number of images. Now i want to add some effects to the images, Transition effects as we mostly have in slide shows e.g. FadeIn or FadeOut. And a background audio also. Is it possible to do with the opencv??

Also i want to show one image at one second delay? the below code is showing four images in one second. I am new to opencv so please help with that.

import cv

im1 = cv.LoadImage("Sunset.jpg")
im2 = cv.LoadImage("Blue hills.jpg")
im3 = cv.LoadImage("Water lilies.jpg")
im4 = cv.LoadImage("Winter.jpg")

fps = 4.0
frame_size = cv.GetSize(im1)

writer = cv.CreateVideoWriter("out.avi", -1, fps, frame_size, True)

for i in range(4):
    print cv.WriteFrame(writer, eval('im' + str(i+1)))

del writer
Aamir Rind
  • 38,793
  • 23
  • 126
  • 164
  • I know nothing about opencv, but wouldn't you want fps = 1.0 to get one image per second? Of course, if you want to generate transition images, you'll want fps to be 4, or 10, or 30, depending on how smooth you want the video to be. – Dave Dec 13 '11 at 13:19
  • yes i also used `fps = 1.0`. It show only single image in one second, but it does not show the rest of images. The video has to be then of 4 seconds as i am adding four frames but it show only one frame. – Aamir Rind Dec 13 '11 at 13:22

2 Answers2

0

Also I want to show one image at one second delay?

Set video frame will help you, if you set frame to 24, then you should

for i in range(len(frame)):
    videoWriter.write(frame)

then you get image showing with a second delay, so also you can set your video frame to 1 and that will cut the video size

videoWriter = cv2.VideoWriter('sample.mp4', 
                               fourcc,
                               1,  # set your Frame-Per-Second here
                              (567, 756))

How to add an audio to a movie file?

In opencv, there is not a very good way to do that, so in hence, I use moviepy to do that:

import moviepy.editor as mpe
my_clip = mpe.VideoFileClip('orginal_video.mp4')
audio_background = mpe.AudioFileClip('music.mp3')
final_audio = mpe.CompositeAudioClip([audio_background])
final_clip = my_clip.set_audio(final_audio)
# audio_codec="aac" could make final_clip played in iPhone/Android/PC
final_clip.write_videofile("final_clip.mp4", audio_codec="aac")
itsmysterybox
  • 2,748
  • 3
  • 21
  • 26
EvinK
  • 131
  • 1
  • 7
0

Yes and No.

OpenCV is not meant for editing videos. I mean, you can do that, but it's not the focus of the library. However, OpenCV offers image processing techniques that might help you achieve those cool effects you are looking for, but it's up to you to write them.

For pausing between frames, check the sleep function (from python's Time library).

OpenCV is aimed at computer vision (audio/video processing), not audio. You might want to take a look at ffmpeg for that. I posted an answer some time ago that shared C code to play audio/video on a SDL window, using a combination of OpenCV to deal with images and FFmpeg to load audio stream from a video file.

Community
  • 1
  • 1
karlphillip
  • 92,053
  • 36
  • 243
  • 426