File size: 2,159 Bytes
d0ffe9c |
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 |
import cv2
import json
import os
import asyncio
async def stylize(video):
command = f"animatediff stylize create-config {video}"
process = await asyncio.create_subprocess_shell(
command,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
stdout, stderr = await process.communicate()
if process.returncode == 0:
return stdout.decode()
else:
print(f"Error: {stderr.decode()}")
async def start_video_edit(prompt_file):
command = f"animatediff stylize generate {prompt_file}"
process = await asyncio.create_subprocess_shell(
command,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
stdout, stderr = await process.communicate()
if process.returncode == 0:
return stdout.decode()
else:
print(f"Error: {stderr.decode()}")
def edit_video(video, pos_prompt):
x = asyncio.run(stylize(video))
x = x.split("stylize.py")
config = x[18].split("config =")[-1].strip()
d = x[19].split("stylize_dir = ")[-1].strip()
with open(config, 'r+') as f:
data = json.load(f)
data['head_prompt'] = pos_prompt
data["path"] = "models/huggingface/xxmix9realistic_v40.safetensors"
os.remove(config)
with open(config, 'w') as f:
json.dump(data, f, indent=4)
out = asyncio.run(start_video_edit(d))
out = out.split("Stylized results are output to ")[-1]
out = out.split("stylize.py")[0].strip()
cwd = os.getcwd()
video_dir = cwd + "/" + out
video_extensions = {'.mp4', '.avi', '.mkv', '.mov', '.flv', '.wmv'}
video_path = None
for dirpath, dirnames, filenames in os.walk(video_dir):
for filename in filenames:
if os.path.splitext(filename)[1].lower() in video_extensions:
video_path = os.path.join(dirpath, filename)
break
if video_path:
break
return video_path
video_path = input("Enter the path to your video: ")
pos_prompt = input("Enter the what you want to do with the video: ")
print("The video is stored at", edit_video(video_path, pos_prompt)) |