{ "cells": [ { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.ipify.org:443\n", "DEBUG:urllib3.connectionpool:https://api.ipify.org:443 \"GET / HTTP/1.1\" 200 15\n", "DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.gradio.app:443\n", "DEBUG:urllib3.connectionpool:https://api.gradio.app:443 \"POST /gradio-initiated-analytics/ HTTP/1.1\" 200 31\n", "DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.gradio.app:443\n", "DEBUG:urllib3.connectionpool:https://api.gradio.app:443 \"POST /gradio-initiated-analytics/ HTTP/1.1\" 200 31\n", "DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.gradio.app:443\n", "DEBUG:urllib3.connectionpool:https://api.gradio.app:443 \"GET /pkg-version HTTP/1.1\" 200 20\n", "DEBUG:asyncio:Using selector: KqueueSelector\n", "DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.ipify.org:443\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Using cache from '/Users/ijanssen/videomatch/gradio_cached_examples/15' directory. If method or examples have changed since last caching, delete this folder to clear cache.\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "DEBUG:urllib3.connectionpool:https://api.ipify.org:443 \"GET / HTTP/1.1\" 200 15\n", "DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.gradio.app:443\n", "DEBUG:urllib3.connectionpool:https://api.gradio.app:443 \"POST /gradio-initiated-analytics/ HTTP/1.1\" 200 31\n", "DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.gradio.app:443\n", "DEBUG:urllib3.connectionpool:https://api.gradio.app:443 \"POST /gradio-initiated-analytics/ HTTP/1.1\" 200 31\n", "DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.gradio.app:443\n", "DEBUG:urllib3.connectionpool:https://api.gradio.app:443 \"GET /pkg-version HTTP/1.1\" 200 20\n", "DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.ipify.org:443\n", "DEBUG:urllib3.connectionpool:https://api.ipify.org:443 \"GET / HTTP/1.1\" 200 15\n", "DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.gradio.app:443\n", "DEBUG:urllib3.connectionpool:https://api.gradio.app:443 \"POST /gradio-initiated-analytics/ HTTP/1.1\" 200 31\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Part Start = 5.0, Part Cutout = 5.0, Part Mid = 5.0, Part End = 135.03\n", "Moviepy - Building video videos/Ploumen_CO_5.0s_to_10.0_at_15.0.mp4.\n", "MoviePy - Writing audio in Ploumen_CO_5.0s_to_10.0_at_15.0TEMP_MPY_wvf_snd.mp4\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ " \r" ] }, { "name": "stdout", "output_type": "stream", "text": [ "MoviePy - Done.\n", "Moviepy - Writing video videos/Ploumen_CO_5.0s_to_10.0_at_15.0.mp4\n", "\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ " \r" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Moviepy - Done !\n", "Moviepy - video ready videos/Ploumen_CO_5.0s_to_10.0_at_15.0.mp4\n" ] } ], "source": [ "import moviepy\n", "from moviepy.editor import concatenate_videoclips\n", "from moviepy.video.io.VideoFileClip import VideoFileClip\n", "from moviepy.audio.io.AudioFileClip import AudioFileClip\n", "from moviepy.audio.AudioClip import CompositeAudioClip\n", "\n", "from app import change_ffmpeg_fps\n", "\n", "def add_logo(input_clip, output_clip_name, logo_file_name, logo_duration, position=(\"right\", \"top\")):\n", " logo = (moviepy.ImageClip(logo_file_name)\n", " .set_duration(logo_duration)\n", " .resize(height=50) # if you need to resize...\n", " .margin(right=8, top=8, opacity=0) # (optional) logo-border padding\n", " .set_pos(position))\n", "\n", " output_clip = moviepy.CompositeVideoClip([input_clip, logo])\n", " output_clip.write_videofile(output_clip_name)\n", " return output_clip\n", "\n", "def edit_remove_part(input_filename, start_s, end_s, FPS=5):\n", " \"\"\"Remove a segment of an .mp4 by and change its FPS. Keeps the audio intact by audio_codec='aac' parameter\"\"\"\n", " # input_video = change_ffmpeg_fps(VideoFileClip(input_filename), fps=FPS) # Load video and change fps for faster processing\n", " input_filename_no_ext, ext = input_filename.split('.')\n", " input_video = VideoFileClip(input_filename)\n", "\n", " # Remove part video and return result\n", " video = input_video.cutout(start_s, end_s)\n", "\n", " # Write Video\n", " output_filename = input_filename_no_ext + f\"_RM_{start_s}s_to_{end_s}.{ext}\"\n", " video.write_videofile(output_filename, audio_codec='aac')\n", "\n", "def edit_change_order(input_filename, start_s, end_s, insert_s, FPS=5):\n", " \"\"\"Take start_s to end_s out of the video and insert it at insert_s. Keeps the audio intact by audio_codec='aac' parameter\"\"\"\n", " # input_video = change_ffmpeg_fps(VideoFileClip(input_filename), fps=FPS) # Load video and change fps for faster processing\n", " input_filename_no_ext, ext = input_filename.split('.')\n", " input_video = VideoFileClip(input_filename)\n", " assert not (insert_s > start_s and insert_s < end_s), \"Insert time can't be in cutout time!\"\n", " assert (end_s > start_s), \"End_s should be higher than start_s!\"\n", "\n", " # Cut out a part and insert it somewhere else\n", " if insert_s < start_s and insert_s < end_s:\n", " part_start = input_video.subclip(0.0, insert_s)\n", " part_cutout = input_video.subclip(start_s, end_s)\n", " part_mid = input_video.subclip(insert_s, start_s)\n", " part_end = input_video.subclip(end_s, input_video.duration)\n", " video = concatenate_videoclips([part_start, part_cutout, part_mid, part_end])\n", " elif insert_s > start_s and insert_s > end_s:\n", " part_start = input_video.subclip(0.0, start_s)\n", " part_cutout = input_video.subclip(start_s, end_s)\n", " part_mid = input_video.subclip(end_s, insert_s)\n", " part_end = input_video.subclip(insert_s, input_video.duration)\n", " print(f\"Part Start = {part_start.duration}, Part Cutout = {part_cutout.duration}, Part Mid = {part_mid.duration}, Part End = {part_end.duration}\")\n", " video = concatenate_videoclips([part_start, part_mid, part_cutout, part_end])\n", "\n", " # Write Video\n", " output_filename = input_filename_no_ext + f\"_CO_{start_s}s_to_{end_s}_at_{insert_s}.{ext}\"\n", " video.write_videofile(output_filename, audio_codec='aac')\n", "\n", "# edit_remove_part(\"videos/Ploumen.mp4\", start_s = 5.0, end_s = 10.0)\n", "# edit_change_order(\"videos/Ploumen.mp4\", start_s = 5.0, end_s = 10.0, insert_s = 15.0)\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[(51, 92), (14, 98), (64, 85), (63, 90), (63, 96)]\n", "[112, 0, 53, 96, 123]\n" ] } ], "source": [ "\n", "import numpy as np\n", "\n", "MAX = 130\n", "\n", "# Get some random start_s and end_s pairs where start_s is always lower than end_s\n", "rand_start_s = np.random.randint(low=0, high=MAX, size=5)\n", "rand_end_s = np.random.randint(low=0, high=MAX, size=5)\n", "rand_pairs = zip(rand_start_s, rand_end_s)\n", "rand_pairs = [(x,y) if (x < y) else (y, x) for x, y in rand_pairs]\n", "\n", "def get_insert_s(start_s, end_s, max=MAX):\n", " \"\"\" Get a insert_s that is outside the start_s and end_s \"\"\"\n", " random_choice = bool(np.random.randint(low=0, high=2, size=1)[0])\n", " if random_choice:\n", " return np.random.randint(low=0, high=start_s, size=1)[0]\n", " else:\n", " return np.random.randint(low=end_s, high=MAX, size=1)[0]\n", "\n", "rand_insert_s = [get_insert_s(x, y) for x, y in rand_pairs]\n", "\n", "print(rand_pairs)\n", "print(rand_insert_s)\n", "\n" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Part Start = 51.0, Part Cutout = 41, Part Mid = 20, Part End = 38.03\n", "Moviepy - Building video videos/Ploumen_CO_51s_to_92_at_112.mp4.\n", "MoviePy - Writing audio in Ploumen_CO_51s_to_92_at_112TEMP_MPY_wvf_snd.mp4\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ " \r" ] }, { "name": "stdout", "output_type": "stream", "text": [ "MoviePy - Done.\n", "Moviepy - Writing video videos/Ploumen_CO_51s_to_92_at_112.mp4\n", "\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ " \r" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Moviepy - Done !\n", "Moviepy - video ready videos/Ploumen_CO_51s_to_92_at_112.mp4\n", "Moviepy - Building video videos/Ploumen_CO_14s_to_98_at_0.mp4.\n", "MoviePy - Writing audio in Ploumen_CO_14s_to_98_at_0TEMP_MPY_wvf_snd.mp4\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ " \r" ] }, { "name": "stdout", "output_type": "stream", "text": [ "MoviePy - Done.\n", "Moviepy - Writing video videos/Ploumen_CO_14s_to_98_at_0.mp4\n", "\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ " \r" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Moviepy - Done !\n", "Moviepy - video ready videos/Ploumen_CO_14s_to_98_at_0.mp4\n", "Moviepy - Building video videos/Ploumen_CO_64s_to_85_at_53.mp4.\n", "MoviePy - Writing audio in Ploumen_CO_64s_to_85_at_53TEMP_MPY_wvf_snd.mp4\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ " \r" ] }, { "name": "stdout", "output_type": "stream", "text": [ "MoviePy - Done.\n", "Moviepy - Writing video videos/Ploumen_CO_64s_to_85_at_53.mp4\n", "\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ " \r" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Moviepy - Done !\n", "Moviepy - video ready videos/Ploumen_CO_64s_to_85_at_53.mp4\n", "Part Start = 63.0, Part Cutout = 27, Part Mid = 6, Part End = 54.03\n", "Moviepy - Building video videos/Ploumen_CO_63s_to_90_at_96.mp4.\n", "MoviePy - Writing audio in Ploumen_CO_63s_to_90_at_96TEMP_MPY_wvf_snd.mp4\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ " \r" ] }, { "name": "stdout", "output_type": "stream", "text": [ "MoviePy - Done.\n", "Moviepy - Writing video videos/Ploumen_CO_63s_to_90_at_96.mp4\n", "\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ " \r" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Moviepy - Done !\n", "Moviepy - video ready videos/Ploumen_CO_63s_to_90_at_96.mp4\n", "Part Start = 63.0, Part Cutout = 33, Part Mid = 27, Part End = 27.03\n", "Moviepy - Building video videos/Ploumen_CO_63s_to_96_at_123.mp4.\n", "MoviePy - Writing audio in Ploumen_CO_63s_to_96_at_123TEMP_MPY_wvf_snd.mp4\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ " \r" ] }, { "name": "stdout", "output_type": "stream", "text": [ "MoviePy - Done.\n", "Moviepy - Writing video videos/Ploumen_CO_63s_to_96_at_123.mp4\n", "\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ " \r" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Moviepy - Done !\n", "Moviepy - video ready videos/Ploumen_CO_63s_to_96_at_123.mp4\n" ] } ], "source": [ "for pair, insert_s in zip(rand_pairs, rand_insert_s):\n", " edit_change_order(\"videos/Ploumen.mp4\", start_s = pair[0], end_s = pair[1], insert_s = insert_s)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3.9.13 64-bit", "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.9.13" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "397704579725e15f5c7cb49fe5f0341eb7531c82d19f2c29d197e8b64ab5776b" } } }, "nbformat": 4, "nbformat_minor": 2 }