有没有办法在Python中正确同步音频视频?

问题描述 投票:0回答:1

我正在尝试用Python 来实现音频和视频自动化。我想将音频文件与视频同步。目前我正在使用 $ 将其分成有暂停的部分。还有其他方法吗?下面是代码

    # Split the text into parts based on consecutive dollar signs ('$')
    text_parts = text.split('$')

    # Initialize a list to store translated and speech-generated audio segments
    audio_segments = []

    # Iterate through text parts
    for part in text_parts:
        stripped_part = part.strip()
        if stripped_part:
            # Translate each part
            translated_part = self.translate_text(stripped_part, target_language)

            # Generate speech for the translated part
            tts = gTTS(translated_part, lang=target_language, slow=False)
            tts.save("temp_part.mp3")
            audio_segment = AudioSegment.from_file("temp_part.mp3")
            audio_segments.append(audio_segment)

    # Calculate pause duration based on the number of consecutive dollar signs
    print(text_parts)
    pause_duration = 500 * len(text_parts)

    # Create silence for the calculated pause duration
    silence = AudioSegment.silent(duration=pause_duration)

    # Combine audio segments with silence to create the final audio
    final_audio = audio_segments[0]
    for segment in audio_segments[1:]:
        final_audio += silence + segment

    # Export the final audio with pauses
    final_audio.export("output_with_pauses.mp3", format="mp3")

    # Generate the video with translated speech
    self.generate_video_with_speech(self.translate_text(text, target_language), target_language)
python audio video synchronization
1个回答
0
投票

您可能需要GStreamer。GStreamer 是一个极其强大且多功能的框架,用于创建流媒体应用程序。还可以帮助您将媒体文件或媒体流从一台设备传递到另一台设备。 GStreamer 提供了各种组件来帮助您构建自己的管道。例如,要同步音频文件,只需执行:

gst-launch-1.0 filesrc location=/path/to/your/file.mp3 ! decodebin ! audioconvert ! audioresample ! lamemp3enc target=bitrate bitrate=192 ! rtpmpapay ! udpsink host=targetIP port=5000

还有另一台机器

gst-launch-1.0 udpsrc port=5000 ! "application/x-rtp, media=(string)audio, clock-rate=(int)90000, encoding-name=(string)MPA" ! rtpmpadepay ! decodebin ! audioconvert ! audioresample ! filesink location=/path/to/save/received/file.mp3
© www.soinside.com 2019 - 2024. All rights reserved.