Delay issue in Audio/Video Merging through Python

1.1k Views Asked by At

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')
0

There are 0 best solutions below