opencv 在Python中添加简单音频库的音频延迟

jmo0nnb3  于 2023-05-29  发布在  Python
关注(0)|答案(1)|浏览(155)

我目前正在尝试同步音频和视频(视频输出来自OpenCV)。我使用simpleaudio函数直接从原始源播放音频。我想在音频中添加一个轻微的延迟,因为音频比OpenCV中的视频更早开始。
下面是我的代码

import cv2 as cv
from pydub.playback import _play_with_simpleaudio

cap = cv.VideoCapture(video_file)
first_loop = True
while cap.isOpened():
        ret, frame = cap.read()
        if not ret: #ret=False means frame is read correctly
            print('\nVideo not being read correctly\n')
            #cap.release()
            break
        
        key_pressed= cv.waitKey(1) 
        # first_loop to start the audio being played
        if first_loop:
            playback= _play_with_simpleaudio(audio)
        #falsify the first loop so that audio doesn't keep playing
        first_loop=False
        #release the cap file
        cap.release()
        #stop the audio from playing
        playback.stop()
ergxz8rk

ergxz8rk1#

您可以尝试在音频文件的开头添加尽可能多的静音。

from pydub import AudioSegment
import time

audio = AudioSegment.from_file("*your_audio_file.wav*", format="wav")

# Set delay amount here
delay = 2000  # 2 seconds

# Create silent
silence = AudioSegment.silent(duration=delay)

# Concatenate 
audio_with_delay = silence + audio

# Export the audio with the delay to a temporary file
audio_with_delay.export("audio_with_delay.wav", format="wav")

现在您可以使用第二个wav文件作为您的音频文件。

相关问题