Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

音画不同步 #917

Open
haomole opened this issue May 29, 2024 · 2 comments
Open

音画不同步 #917

haomole opened this issue May 29, 2024 · 2 comments

Comments

@haomole
Copy link

haomole commented May 29, 2024

  • 画面正常 声音延迟
@eoffermann
Copy link

Translation: The picture is normal but the sound is delayed.

@haomole
Copy link
Author

haomole commented Jun 7, 2024

I have fixed this issue:

src\utils\videoio.py:

import shutil
import uuid
import os
import cv2

def load_video_to_cv2(input_path):
    video_stream = cv2.VideoCapture(input_path)
    fps = video_stream.get(cv2.CAP_PROP_FPS)
    full_frames = []
    while True:
        still_reading, frame = video_stream.read()
        if not still_reading:
            video_stream.release()
            break
        full_frames.append(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
    return full_frames, fps

def change_frame_rate(input_path, output_path, fps):
    temp_file = str(uuid.uuid4()) + '.mp4'
    cmd = (r'ffmpeg -y -hide_banner -loglevel error -i "%s" -r %d -qscale 0 "%s"') % (input_path, fps, temp_file)
    os.system(cmd)
    shutil.move(temp_file, output_path)

def save_video_with_watermark(video, audio, save_path, watermark=False, target_fps=30):
    temp_video = str(uuid.uuid4()) + '_video.mp4'
    change_frame_rate(video, temp_video, target_fps)

    temp_file = str(uuid.uuid4()) + '.mp4'
    cmd = (r'ffmpeg -i "%s" -i "%s" '
       r'-c:v libx264 -b:v 5000k -maxrate 5000k -bufsize 10000k '
       r'-c:a aac -b:a 122k -ac 2 -ar 44100 -strict experimental "%s" ') % (temp_video, audio, temp_file)


    os.system(cmd)

    try:
        if not watermark:
            shutil.move(temp_file, save_path)
        else:
            try:
                # check if stable-diffusion-webui
                import webui
                from modules import paths
                watermark_path = paths.script_path + "/extensions/SadTalker/docs/sadtalker_logo.png"
            except ImportError:
                # get the root path of sadtalker
                dir_path = os.path.dirname(os.path.realpath(__file__))
                watermark_path = dir_path + "/../../docs/sadtalker_logo.png"

            cmd = (r'ffmpeg -y -hide_banner -loglevel error -i "%s" -i "%s" '
                   r'-filter_complex "[1]scale=100:-1[wm];[0][wm]overlay=(main_w-overlay_w)-10:10" -c:v copy -c:a aac -b:a 128k -ar 16000 -shortest "%s"') % (temp_file, watermark_path, save_path)
            os.system(cmd)
            os.remove(temp_file)
    except Exception as e:
        print("Error:", e)
    finally:
        os.remove(temp_video)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants