{ "cells": [ { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Clips: ['1.mp4', '1.mp4']\n" ] }, { "data": { "text/plain": [ "['1.mp4', '1.mp4']" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "files = {\n", " 1: \"1.mp4\", 2: \"2.mp4\", 3: \"3.mp4\", 4: \"4.mp4\", 5: \"5.mp4\",\n", " 6: \"6.mp4\", 7: \"7.mp4\", 8: \"8.mp4\", 9: \"9.mp4\",\n", " 10: \"10.mp4\", 11: \"11.mp4\", 12: \"12.mp4\", 13: \"13.mp4\", 14: \"14.mp4\",\n", " 15: \"15.mp4\", 16: \"16.mp4\", 17: \"17.mp4\", 18: \"18.mp4\", 19: \"19.mp4\",\n", " 20: \"20.mp4\", 30: \"30.mp4\", 40: \"40.mp4\", 50: \"50.mp4\", \n", " 60: \"60.mp4\", 70: \"70.mp4\", 80: \"80.mp4\", 90: \"90.mp4\",\n", " 100: \"100.mp4\", 1000: \"1000.mp4\",\n", " 100000: \"100000.mp4\"\n", "}\n", "\n", "def get_clips(number):\n", " num_digits = len(str(number))\n", " place_values = []\n", " place = 1\n", "\n", " # Split number into place values\n", " while number > 0:\n", " digit = number % 10\n", " if digit > 0:\n", " place_values.append((digit, place))\n", " number //= 10\n", " place *= 10\n", " \n", " place_values.reverse() # To process from the highest place value\n", " clips = []\n", " i = 0\n", "\n", " while i < len(place_values):\n", " digit, place = place_values[i]\n", "\n", " # Handle cases for 1-20 at specific positions\n", " if place == 1 and i > 0 and place_values[i - 1][1] == 10: # Check for numbers ending in 1-20\n", " combined = place_values[i - 1][0] * 10 + digit\n", " if 1 <= combined <= 20:\n", " clips.pop() # Remove the previous 10's place clip\n", " clips.append(files[combined])\n", " i += 1\n", " continue\n", " \n", " if place == 10 and digit * 10 <= 20: # Numbers between 1-20 at higher positions\n", " if i + 1 < len(place_values) and place_values[i + 1][1] == 1:\n", " combined = digit * 10 + place_values[i + 1][0]\n", " if 1 <= combined <= 20:\n", " clips.append(files[combined])\n", " i += 2\n", " continue\n", "\n", " # Add individual clips\n", " if digit in files:\n", " clips.append(files[digit])\n", " if digit * place in files:\n", " clips.append(files[digit * place])\n", "\n", " i += 1\n", "\n", " # Handle 10,000 and 10,00,000 special cases\n", " if len(place_values) % 2 == 1 and place_values[0][1] in [10000, 100000]:\n", " first_digit = place_values[0][0]\n", " if 1 <= first_digit <= 20:\n", " clips.insert(0, files[first_digit])\n", " elif first_digit * 10 in files:\n", " clips.insert(0, files[first_digit * 10])\n", " place_values = place_values[1:]\n", "\n", " print(\"Clips:\", clips)\n", " return clips\n", "\n", "# Example usage\n", "number = int(input(\"Enter a number: \"))\n", "get_clips(number)\n" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "99: ['90.mp4', '9.mp4']\n", "111: ['1.mp4', '100.mp4', '11.mp4']\n", "256: ['2.mp4', '100.mp4', '50.mp4', '6.mp4']\n", "1000: ['1.mp4', '1000.mp4']\n", "1011: ['1.mp4', '1000.mp4', '11.mp4']\n", "9999: ['9.mp4', '1000.mp4', '9.mp4', '100.mp4', '90.mp4', '9.mp4']\n", "10000: ['10.mp4', '1000.mp4']\n", "24256: ['20.mp4', '4.mp4', '1000.mp4', '2.mp4', '100.mp4', '50.mp4', '6.mp4']\n", "22: ['20.mp4', '2.mp4']\n", "19: ['19.mp4']\n", "4: ['4.mp4']\n" ] } ], "source": [ "files = {\n", " 1: \"1.mp4\", 2: \"2.mp4\", 3: \"3.mp4\", 4: \"4.mp4\", 5: \"5.mp4\",\n", " 6: \"6.mp4\", 7: \"7.mp4\", 8: \"8.mp4\", 9: \"9.mp4\",\n", " 10: \"10.mp4\", 11: \"11.mp4\", 12: \"12.mp4\", 13: \"13.mp4\", 14: \"14.mp4\",\n", " 15: \"15.mp4\", 16: \"16.mp4\", 17: \"17.mp4\", 18: \"18.mp4\", 19: \"19.mp4\",\n", " 20: \"20.mp4\", 30: \"30.mp4\", 40: \"40.mp4\", 50: \"50.mp4\",\n", " 60: \"60.mp4\", 70: \"70.mp4\", 80: \"80.mp4\", 90: \"90.mp4\",\n", " 100: \"100.mp4\", 1000: \"1000.mp4\",\n", " 100000: \"100000.mp4\"\n", "}\n", "\n", "def number_to_clips(n):\n", " components = []\n", " if n >= 100000:\n", " lakh = n // 100000\n", " if lakh > 0:\n", " components.append(lakh)\n", " components.append(100000)\n", " n %= 100000\n", " if n >= 1000:\n", " thousand = n // 1000\n", " if thousand > 0:\n", " components.append(thousand)\n", " components.append(1000)\n", " n %= 1000\n", " if n >= 100:\n", " hundred = n // 100\n", " if hundred > 0:\n", " components.append(hundred)\n", " components.append(100)\n", " n %= 100\n", " if n > 20:\n", " tens = (n // 10) * 10\n", " if tens > 0:\n", " components.append(tens)\n", " n %= 10\n", " if n > 0:\n", " components.append(n)\n", " return components\n", "\n", "def generate_clip_sequence(n):\n", " components = number_to_clips(n)\n", " clip_sequence = []\n", " for component in components:\n", " if component in files:\n", " clip_sequence.append(files[component])\n", " else:\n", " # Break down further if component is not in files\n", " sub_components = number_to_clips(component)\n", " clip_sequence.extend([files[sub] for sub in sub_components])\n", " return clip_sequence\n", "\n", "# Example usage\n", "examples = [99, 111, 256, 1000, 1011, 9999, 10000, 24256, 22, 19, 4]\n", "for example in examples:\n", " print(f\"{example}: {generate_clip_sequence(example)}\")\n" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "# code to remove first few and last few frames of clips using moviepy\n", "import os\n", "from moviepy.editor import VideoFileClip\n", "\n", "def trim_clips(clips, start=0, end=0):\n", " trimmed_clips = []\n", " for clip in clips:\n", " clip_path = os.path.join(\"clips\", clip)\n", " video = VideoFileClip(clip_path)\n", " trimmed = video.subclip(start, video.duration - end)\n", " trimmed_clips.append(trimmed)\n", " return trimmed_clips\n", "\n", "# Example usage\n", "clips = os.listdir(\"clips\")\n", "clips = [clip for clip in clips if clip.endswith(\".mp4\")]\n", "\n", "# Trim first 1 second and last 2 seconds\n", "trimmed_clips = trim_clips(clips, 0.3, 0.3)\n" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [], "source": [ "payload = {\n", " \"title\": \"Sample Avatar Video\",\n", " \"caption\": False,\n", " \"callback_id\": \"your_custom_id_123\",\n", " \"dimension\": {\n", " \"width\": 720,\n", " \"height\": 1280\n", " },\n", " \"video_inputs\": [\n", " {\n", " \"character\": {\n", " \"type\": \"avatar\",\n", " \"avatar_id\": \"42b4f9358cc9443a8610fda90bd3ad87\",\n", " \"scale\": 1.0,\n", " \"avatar_style\": \"normal\",\n", " \"offset\": {\n", " \"x\": 0.0,\n", " \"y\": 0.0\n", " },\n", " \"matting\": False\n", " },\n", " \"voice\": {\n", " \"type\": \"audio\",\n", " \"audio_url\": \"https://drive.google.com/file/d/1CF8TjWwPWiIPJX3D1bjpMopinHt4iw1D/view?usp=sharing\"\n", " },\n", " }\n", " ],\n", " \"callback_url\": \"https://your-callback-url.com\"\n", "}" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [], "source": [ "payload = {\n", " \"title\": \"Sample Talking Photo Video\",\n", " \"caption\": False,\n", " \"callback_id\": \"your_custom_id_123\",\n", " \"dimension\": {\n", " \"width\": 720,\n", " \"height\": 1280\n", " },\n", " \"video_inputs\": [\n", " {\n", " \"character\": {\n", " \"type\": \"talking_photo\",\n", " \"talking_photo_id\": 'cf557bdc461b487fb703b0c3138df325'\n", " },\n", " \"voice\": {\n", " \"type\": \"audio\",\n", " \"audio_url\": \"https://drive.google.com/file/d/1CF8TjWwPWiIPJX3D1bjpMopinHt4iw1D/view?usp=sharing\"\n", " },\n", " \"talking_style\": \"stable\",\n", " \"expression\": \"happy\",\n", " # \"background\": {\n", " # \"type\": \"color\",\n", " # \"value\": \"#FAFAFA\"\n", " # }\n", " }\n", " ]\n", "}\n" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\"error\": null, \"data\": {\"video_id\": \"d8194dfd76fa40c49bfc9a99c67ffbe3\"}}\n" ] } ], "source": [ "import requests\n", "\n", "url = \"https://api.heygen.com/v2/video/generate\"\n", "\n", "\n", "headers = {\n", " \"accept\": \"application/json\",\n", " \"content-type\": \"application/json\",\n", " \"x-api-key\": \"YTBjYzk5MjI1ZWQzNGViOTgxMTI0NjM2OGQ5NDc2OGEtMTczMzk5MjA5MA==\"\n", "}\n", "\n", "response = requests.post(url, json=payload, headers=headers)\n", "\n", "print(response.text)\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'d8194dfd76fa40c49bfc9a99c67ffbe3'" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "video_id = response.json()[\"data\"][\"video_id\"]\n", "video_id" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [], "source": [ "url = \"https://api.heygen.com/v1/video_status.get\"\n", "\n", "headers = {\n", " \"accept\": \"application/json\",\n", " \"x-api-key\": \"YTBjYzk5MjI1ZWQzNGViOTgxMTI0NjM2OGQ5NDc2OGEtMTczMzk5MjA5MA==\"\n", "}\n", "\n", "\n", "response = requests.get(url, headers=headers, params={\"video_id\": video_id}).json()" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'code': 100,\n", " 'data': {'callback_id': None,\n", " 'caption_url': None,\n", " 'created_at': 1736231063,\n", " 'duration': None,\n", " 'error': None,\n", " 'gif_url': None,\n", " 'id': 'd8194dfd76fa40c49bfc9a99c67ffbe3',\n", " 'status': 'processing',\n", " 'thumbnail_url': None,\n", " 'video_url': None,\n", " 'video_url_caption': None},\n", " 'message': 'Success'}" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "response" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "100" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "response[\"code\"]" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "video_url = response.get(\"data\").get(\"video_url\")" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "downloaded_video = requests.get(video_url)\n", "\n", "# save the video to a file\n", "with open(\"UPIBOT.mp4\", \"wb\") as f:\n", " f.write(downloaded_video.content)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\n", "import json\n", "from moviepy.editor import VideoFileClip\n", "\n", "# Load the clips metadata\n", "with open(\"clips_metadata.json\", \"r\") as f:\n", " clips_metadata = json.load(f)\n", "\n", "# Load the downloaded video\n", "video = VideoFileClip(\"UPIBOT.mp4\")\n", "\n", "\n", "# use that video to generate short clips from using timestamps mentioned in clips_metadata\n", "clips = []\n", "for clip_data in clips_metadata:\n", " start_time = clip_data[\"start_time_ms\"]\n", " end_time = clip_data[\"end_time_ms\"]\n", "\n", " # convert milliseconds to seconds\n", " start_time /= 1000\n", " end_time /= 1000\n", "\n", " try:\n", " clip_name = clip_data[\"file_name\"].split(\".\")[0]+\".mp4\"\n", " clip = video.subclip(start_time, end_time)\n", " clips.append(clip)\n", " except Exception as e:\n", " print(f\"Error in clip {clip_name}: {e}\")\n", "\n", "# save the clips to a dir\n", "\n", "import os\n", "clips_dir = str(input(\"Enter the directory to save the clips: \"))\n", "os.makedirs(clips_dir, exist_ok=True)\n", "\n", "for i, clip in enumerate(clips):\n", " clip_path = os.path.join(clips_dir, clips_metadata[i][\"file_name\"].split(\".\")[0]+\".mp4\")\n", " clip.write_videofile(clip_path, codec=\"libx264\", audio_codec=\"aac\")" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [], "source": [ "import json\n", "with open(\"clips_metadata.json\", \"r\") as f:\n", " clips_metadata = json.load(f)\n", "\n", "for item in clips_metadata:\n", " item[\"start_time_ms\"]=item[\"start_time_ms\"]+20\n", " item[\"end_time_ms\"]=item[\"end_time_ms\"]-20\n", "\n", "# update the file with new timestamps\n", "with open(\"clips_metadata.json\", \"w\") as f:\n", " json.dump(clips_metadata, f, indent=4)" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'code': 100, 'data': {'talking_photo_id': 'cf557bdc461b487fb703b0c3138df325', 'talking_photo_url': 'https://files2.heygen.ai/prod/movio/url_upload/user_upload/a0cc99225ed34eb9811246368d94768a/f670829259c24a0197966e9f7c767ada.image/png?Expires=1736835408&Signature=EcHmA-5R6USvUeus0Ku~NCoCfg~T9NteKjfR59HEYOOurb7in9T4QB3Um8aeHBGLhIz1~5epJuvzIVnuB9H2Ab5uwV9B84KdXWK5rWhrCefRRn8JQxFOnvoCDtQFeHhuTs8957xbboJkFdLiv8OLli90lkmO0fzZecs7WB~WFFhQGqeAhCPjAvQt325ECSngNA2Z4MbI96YbOanE5GiRvUg56hczdb9XqhZktDkZKIDZGl3jk45BcmoaYh6N~sySnGN4kBe-Ny8exFzH0cjloX3gwkHnwsitmI2yQKmse7LKQFEt8WyQYxQ6z6q4YN1FpjKqMjK1T~~UGLepdGIDCA__&Key-Pair-Id=K38HBHX5LX3X2H'}, 'msg': None, 'message': None}\n" ] } ], "source": [ "api_key = \"YTBjYzk5MjI1ZWQzNGViOTgxMTI0NjM2OGQ5NDc2OGEtMTczMzk5MjA5MA==\"\n", "\n", "with open(\"man_portrait.png\", \"rb\") as f:\n", " resp = requests.post(\"https://upload.heygen.com/v1/talking_photo\", data=f, headers={\"Content-Type\": \"image/png\", \"x-api-key\": api_key})\n", " print(resp.json())" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'cf557bdc461b487fb703b0c3138df325'" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "response = resp.json()\n", "response[\"data\"][\"talking_photo_id\"]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\n", "url = \"https://api.heygen.com/v1/talking_photo.list\"\n", "\n", "headers = {\n", " \"accept\": \"application/json\",\n", " \"x-api-key\": api_key\n", "}\n", "\n", "response = requests.get(url, headers=headers)\n", "\n", "print(response.text)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "venv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.3" } }, "nbformat": 4, "nbformat_minor": 2 }