{ "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "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": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3.9.7 ('base')", "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.7" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "35ac539f20c4edc7c4b10c8a5969be22a35cbd7bf12b66c83932160e8a573333" } } }, "nbformat": 4, "nbformat_minor": 2 }