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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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
{
 "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.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
}