Spaces:
Sleeping
Sleeping
File size: 2,245 Bytes
04c1e71 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
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 1:
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
def save_video_with_watermark(video, audio, save_path, watermark=False):
temp_file = str(uuid.uuid4())+'.mp4'
from moviepy.editor import VideoFileClip, AudioFileClip
import tempfile
import base64
# Load audio and video files
audio_clip = AudioFileClip(audio)
video_clip = VideoFileClip(video)
# Combine audio and video
final_clip = video_clip.set_audio(audio_clip)
temp_file = tempfile.NamedTemporaryFile(suffix='.mp4', delete=False)
temp_file.close() # Close the file so it can be opened by MoviePy
temp_file_path = temp_file.name
final_clip.write_videofile(temp_file_path)
with open(temp_file_path, 'rb') as f:
video_content = f.read()
base64_video = base64.b64encode(video_content).decode('utf-8')
return base64_video, temp_file_path
# cmd = r'ffmpeg -y -i "%s" -i "%s" -vcodec copy "%s"' % (video, audio, temp_file)
# os.system(cmd)
# if watermark is False:
# shutil.move(temp_file, save_path)
# else:
# # watermark
# try:
# ##### check if stable-diffusion-webui
# import webui
# from modules import paths
# watarmark_path = paths.script_path+"/extensions/SadTalker/docs/sadtalker_logo.png"
# except:
# # get the root path of sadtalker.
# dir_path = os.path.dirname(os.path.realpath(__file__))
# watarmark_path = dir_path+"/../../docs/sadtalker_logo.png"
# cmd = r'ffmpeg -y -hide_banner -i "%s" -i "%s" -filter_complex "[1]scale=100:-1[wm];[0][wm]overlay=(main_w-overlay_w)-10:10" "%s"' % (temp_file, watarmark_path, save_path)
# os.system(cmd)
# os.remove(temp_file) |