TheNetherWatcher/Vid2Vid-using-Text-prompt
6
1import cv22import json3import os4import asyncio5 6async def stylize(video):7 command = f"animatediff stylize create-config {video}"8 process = await asyncio.create_subprocess_shell(9 command,10 stdout=asyncio.subprocess.PIPE,11 stderr=asyncio.subprocess.PIPE12 )13 stdout, stderr = await process.communicate()14 if process.returncode == 0:15 return stdout.decode()16 else:17 print(f"Error: {stderr.decode()}")18 19async def start_video_edit(prompt_file):20 command = f"animatediff stylize generate {prompt_file}"21 process = await asyncio.create_subprocess_shell(22 command,23 stdout=asyncio.subprocess.PIPE,24 stderr=asyncio.subprocess.PIPE25 )26 stdout, stderr = await process.communicate()27 if process.returncode == 0:28 return stdout.decode()29 else:30 print(f"Error: {stderr.decode()}")31 32def edit_video(video, pos_prompt): 33 x = asyncio.run(stylize(video))34 x = x.split("stylize.py")35 config = x[18].split("config =")[-1].strip()36 d = x[19].split("stylize_dir = ")[-1].strip()37 38 with open(config, 'r+') as f:39 data = json.load(f)40 data['head_prompt'] = pos_prompt41 data["path"] = "models/huggingface/xxmix9realistic_v40.safetensors"42 43 os.remove(config)44 with open(config, 'w') as f:45 json.dump(data, f, indent=4)46 47 out = asyncio.run(start_video_edit(d))48 out = out.split("Stylized results are output to ")[-1]49 out = out.split("stylize.py")[0].strip()50 51 cwd = os.getcwd()52 video_dir = cwd + "/" + out53 54 video_extensions = {'.mp4', '.avi', '.mkv', '.mov', '.flv', '.wmv'}55 video_path = None56 57 for dirpath, dirnames, filenames in os.walk(video_dir):58 for filename in filenames:59 if os.path.splitext(filename)[1].lower() in video_extensions:60 video_path = os.path.join(dirpath, filename)61 break62 if video_path:63 break64 65 return video_path66 67video_path = input("Enter the path to your video: ")68pos_prompt = input("Enter the what you want to do with the video: ")69print("The video is stored at", edit_video(video_path, pos_prompt))