2

I am writing a program in python on RaspberryPi, to record video on a Picam and capture audio through a Microphone attached with USB AUDIO DEVICE.

On combining both files through ffmpeg, till 30sec both Audio/video synchronize well but after 30sec, I start to experiance a delay problem.

Can you please guide me in resolving this issue?

import picamera, subprocess, os
import pyaudio,wave,sys

CHUNK = 8192
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
RECORD_SECONDS = 60                                                 #"Recording Duration"
WAVE_OUTPUT_FILENAME = 'Audio.wav'
H264_OUTPUT_FILENAME = 'Video.h264'
frames = []
with picamera.PiCamera() as camera:
    camera.resolution= (640,480)
    p = pyaudio.PyAudio()
    stream = p.open(format=FORMAT,
                    channels = CHANNELS,
                    rate = RATE,
                    input = True,
                    input_device_index = 0,
                    frames_per_buffer = CHUNK)
    camera.start_preview(fullscreen=False, window=(930, 0, 425, 295))# "Start Video Preview"
    camera.start_recording(H264_OUTPUT_FILENAME, quality = 30)                     # "Start Video Recording"
    # "Start Audio Dubbing"        
    for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
        data = stream.read(CHUNK)
        frames.append(data)
    stream.stop_stream()                                             # "Pause the Stream"
    stream.close()                                                   # "Stream Stop"
    p.terminate()                                                    # "Stream Cloase"
    camera.stop_recording()                                          # "Pause" the recording
    camera.stop_preview()                                            # "Stop Preview"
    camera.close()                                                   # "Camera Close"
# Creation of MIC WAVE FILE
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')                              
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()
# Combining/Merging of Audio/Video File into mkv
cmd = 'ffmpeg -y -i Audio.wav  -r 30 -i Video.h264  -filter:a aresample=async=1 -c:a flac -c:v copy av.mkv'
subprocess.call(cmd, shell=True)                                     # "Muxing Done
print('Muxing Done')
1
1

The problem is, as Irfan Ghaffar points out, that the raspberry pi camera module does not, in fact, produce 25 frames per second. Instead, the frequency determined by the oscillator within is more like 24.5 Hz, which, at increasing video length, produces unexpected surplus of frames.

The current solution Irfan links to is to consider the frame rate of video input as 25.375 FPS (or roughly so).

So the -r switch to ffmpeg should be something 30.123 where 123 is a bit trial and error. See the thread above for more info.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.