File size: 5,820 Bytes
b117448
 
 
 
eccc5cd
b117448
 
 
 
 
 
 
eccc5cd
b117448
 
 
 
 
 
 
eccc5cd
b117448
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eccc5cd
b117448
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eccc5cd
b117448
 
 
 
 
 
 
 
 
 
 
 
 
eccc5cd
b117448
 
 
 
eccc5cd
b117448
 
 
 
 
 
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
{
 "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
}