Last active
December 6, 2020 13:01
-
-
Save jonathanperret/0e49159ffefe7c05fb4f4cd9e0c90ad5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 164, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Requirement already satisfied: hurry.filesize in /opt/conda/lib/python3.8/site-packages (0.9)\n", | |
"Requirement already satisfied: setuptools in /opt/conda/lib/python3.8/site-packages (from hurry.filesize) (49.6.0.post20201009)\n" | |
] | |
} | |
], | |
"source": [ | |
"import sys\n", | |
"!{sys.executable} -m pip install hurry.filesize" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 82, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import proglog\n", | |
"proglog.notebook()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 149, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from moviepy.editor import *\n", | |
"\n", | |
"srcclip = VideoFileClip(\"./sushi-bar-fresh-samples.mp4\", audio=False, target_resolution=(512,None))\n", | |
"srcclip = srcclip.fx(vfx.crop, y1=srcclip.h - srcclip.w*9/16)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 150, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"audio = AudioFileClip(\"sounds-like-an-earful.wav\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 151, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[37.604, 60.144, 68.038, 92.862, 96.941, 98.945, 104.932, 107.341, 118.868, 122.251, 124.98, 134.294, 140.768, 145.446, 171.927, 174.237, 189.078, 192.239, 201.358, 205.785, 208.988, 214.474, 218.162, 224.108, 231.765, 237.097, 244.698, 248.778, 255.595, 258.365, 266.983, 273.665, 277.383, 280.599, 285.36]\n" | |
] | |
} | |
], | |
"source": [ | |
"from pathlib import Path\n", | |
"from datetime import datetime\n", | |
"import re\n", | |
"log = Path(\"sushi-bar-fresh-samples.txt\").read_text()\n", | |
"lines = log.splitlines()\n", | |
"\n", | |
"events = []\n", | |
"\n", | |
"for line in lines:\n", | |
" if re.search(r\"Loading [^/]+/[^/]+$\", line):\n", | |
" ts0 = datetime.strptime(line.split(\" \")[1], \"%H:%M:%S.%f\")\n", | |
" if re.search(r\"Snapping\", line):\n", | |
" ts = datetime.strptime(line.split(\" \")[1], \"%H:%M:%S.%f\") - ts0\n", | |
" events.append(ts.total_seconds())\n", | |
" \n", | |
"print(events)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 153, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"event_offset = 2.5\n", | |
"extract_duration = 1.5\n", | |
"final_extract_duration = 5\n", | |
"extracts = [srcclip.subclip(event_offset + t, event_offset + t + extract_duration) for t in events[:-1]]\n", | |
"extracts.append(srcclip.subclip(event_offset + events[-1], event_offset + events[-1] + final_extract_duration))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 156, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"fade_duration=0.1\n", | |
"fade_color=[0,0,0]\n", | |
"\n", | |
"def fade_concat(clips):\n", | |
" fades = []\n", | |
" start = 0\n", | |
" for i in range(0, len(clips)):\n", | |
" clip = clips[i]\n", | |
" if i < len(clips) - 1:\n", | |
" clip = clip.fx(vfx.fadeout, duration=fade_duration, final_color=fade_color)\n", | |
" if i > 0:\n", | |
" clip = clip.fx(vfx.fadein, duration=fade_duration, initial_color=fade_color)\n", | |
" clip = clip.with_start(start)\n", | |
" start = start + clip.duration\n", | |
" fades.append(clip)\n", | |
" return CompositeVideoClip(fades)\n", | |
"\n", | |
"concatenated = fade_concat(extracts).fx(vfx.fadein, duration=.5).fx(vfx.fadeout, duration=.5)\n", | |
"\n", | |
"audio_clipped = audio.with_duration(concatenated.duration).audio_fadein(1).audio_fadeout(1)\n", | |
"\n", | |
"outclip = concatenated.with_audio(audio_clipped)\n", | |
"\n", | |
"# outclip.ipython_display(autoplay=True, maxduration=120, width=1024)\n", | |
"\n", | |
"outclip.write_videofile(\"out.mp4\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 168, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"4M\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/html": [ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment