codeShare commited on
Commit
ec599cc
·
verified ·
1 Parent(s): df1eead

Upload Video to Keyframe Extractor.ipynb

Browse files
Files changed (1) hide show
  1. Video to Keyframe Extractor.ipynb +1 -1
Video to Keyframe Extractor.ipynb CHANGED
@@ -1 +1 @@
1
- {"cells":[{"cell_type":"code","execution_count":null,"metadata":{"id":"Dwr7gk5OwuGC"},"outputs":[],"source":["from google.colab import drive\n","drive.mount('/content/drive')"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"wTbO9mWbDXNr"},"outputs":[],"source":["\n","# @markdown 🌊 Rapid keyframe processing\n","# @markdown <br> ------ <br> Extract Keyframes from ALL mp4 / webm videos found on Google Drive\n","# @markdown <br> Be mindful of Google Drive Terms of Service\n","# @markdown <br> This cell will process all mp4 videos found under\n","# @markdown <br> /content/drive/MyDrive/Saved from Chrome/\n","delete_mp4_when_done = True # @param {type:'boolean'}\n","# @markdown <br> deleted mp4/webm files will be found under 'trash' in your Google drive\n","# @markdown <br> -------\n","# @markdown <br> (Optional) Add a direct video link to below field.\n","# @markdown <br> Multiple links can be written in this field\n","# @markdown <br> separated by comma. Like this: <br> ' https:\\\\\\my_video.mp4 , https:\\\\\\second_video.webm , .... '\n","import os\n","import shutil\n","!pip install video-kf\n","!pip install ffmpeg-python\n","!pip install wget\n","import wget\n","import videokf as vf\n","import time\n","#def mkdirs(folder):\n","# if not os.path.exists(folder):os.makedirs(folder)\n","#----#\n","direct_link = '' # @param {type:'string'}\n","# @markdown The linked videos will be downloaded to the Google drive prior to running the script.\n","# @markdown <br> This feature is useful for direct processing .webm from 4chan threads into keyframes\n","use_link = False\n","if direct_link.find('http')>-1: use_link = True\n","if use_link:\n"," %cd '/content/drive/MyDrive/Saved from Chrome/'\n"," for link in direct_link.split(','):\n"," if not link.find('http')>-1:continue\n"," wget.download(link.strip())\n"," time.sleep(5)\n"," %cd '/content/'\n","#-----#\n","filenames = []\n","srcpath = '/content/drive/MyDrive/Saved from Chrome/'\n","destpath = '/content/drive/MyDrive/'\n","localpath = '/content/'\n","converted = ''\n","for filename in os.listdir(f'{srcpath}'):\n"," for suffix in ['.mp4','.webm']:\n"," if filename.find(f'{suffix}')>-1: filenames.append(filename)\n","#Rename the downloaded video to 'tgt0' before running this cell\n","def my_mkdirs(folder):\n"," if os.path.exists(folder):shutil.rmtree(folder)\n"," os.makedirs(folder)\n","#----#\n","# @markdown Write a funny name for the folder(s) containing the keyframes\n","name_keyframes_as='' # @param {type:'string'}\n","# @markdown Created .zip files will not be overwritten\n","#NUM_ITEMS = 1 # @param {type:'slider', min:1 , max:20,step:1}\n","if name_keyframes_as.strip()=='': name_keyframes_as='keyframes'\n","num = 0\n","savepath = ''\n","for filename in filenames:\n"," tgt_folder = f'/content/tmp'\n"," my_mkdirs(f'{tgt_folder}')\n"," print(f'Now processing video {filename}...')\n"," vf.extract_keyframes(f'{srcpath}{filename}',output_dir_keyframes=f'{tgt_folder}')\n"," savepath = f'{destpath}{name_keyframes_as}_v{num}_kf'\n"," #---#\n"," while os.path.exists(f'{savepath}.zip'):\n"," #print(f'{savepath}.zip already exists...')\n"," num = num+1\n"," savepath = f'{destpath}{name_keyframes_as}_v{num}_kf'\n"," #---#\n"," shutil.make_archive(savepath,'zip' , f'{tgt_folder}')\n"," if delete_mp4_when_done: os.remove(f'{srcpath}{filename}')\n"," num = num+1\n"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"DlwPZ1pMyp9t"},"outputs":[],"source":["\n","# @markdown Extract Keyframes from video uploaded in '/content/'\n","import os\n","import shutil\n","!pip install video-kf\n","#Rename the downloaded video to 'tgt' before running this cell\n","def my_mkdirs(folder):\n"," if os.path.exists(folder):shutil.rmtree(folder)\n"," os.makedirs(folder)\n","#----#\n","num=0\n","tgt_folder = f'/content/tmp/'\n","my_mkdirs(f'{tgt_folder}')\n","!video-kf '/content/tgt' -dir '/content/' -o '/content/tmp/'\n","shutil.make_archive('/content/images'+f'{num}' ,'zip' , f'{tgt_folder}')\n","num = num+1\n","\n"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"hNbVdLKcGluC"},"outputs":[],"source":["# @markdown Extract Keyframes from video stored on Google Drive\n","# @markdown <br> Be mindful of Google Drive Terms of Service\n","import os\n","import shutil\n","!pip install video-kf\n","#Rename the downloaded video to 'tgt' before running this cell\n","def my_mkdirs(folder):\n"," if os.path.exists(folder):shutil.rmtree(folder)\n"," os.makedirs(folder)\n","#----#\n","num=0\n","tgt_folder = f'/content/tmp/'\n","my_mkdirs(f'{tgt_folder}')\n","!video-kf '/content/drive/MyDrive/Saved from Chrome/tgt' -dir '/content/' -o '/content/tmp/'\n","shutil.make_archive('/content/drive/MyDrive/images'+f'{num}' ,'zip' , f'{tgt_folder}')\n","num = num+1\n"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"bk4rg4zhCqZv"},"outputs":[],"source":["# @markdown .zip and upload keyframes on Google Drive\n","# @markdown <br> Be mindful of Google Drive Terms of Service\n","shutil.make_archive('/content/drive/MyDrive/images'+f'{num}' ,'zip' , f'{tgt_folder}')"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"J811UZU6xZEo"},"outputs":[],"source":["\n","# @markdown Convert all uploaded images to '/content/'\n","# @markdown <br> to the more compact .webp format\n","# @markdown and place them in the '/content/tmp' folder\n","import os\n","from PIL import Image\n","home_directory = '/content/'\n","using_Kaggle = os.environ.get('KAGGLE_URL_BASE','')\n","if using_Kaggle : home_directory = '/kaggle/working/'\n","%cd {home_directory}\n","#----#\n","def my_mkdirs(folder):\n"," if os.path.exists(folder)==False:\n"," os.makedirs(folder)\n","#----#\n","tgt_folder = f'/content/tmp/'\n","src_folder = '/content/' # @param\n","my_mkdirs(f'{tgt_folder}')\n","#---#\n","suffixes = ['.jpg','.gif', '.jpeg','.WEBP','.PNG','.png', '.JPEG' , '.webp' , '.jpg']\n","num = 1\n","for filename in os.listdir(src_folder):\n"," for suffix in suffixes:\n"," if not filename.find(suffix)>-1: continue\n"," print(filename)\n"," %cd {src_folder}\n"," input_image = Image.open(f\"{filename}\").convert('RGB')\n"," %cd {tgt_folder}\n"," input_image.save(f'{num}.webp', \"WEBP\")\n"," num = num+1"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"5EztLCjkPq4U"},"outputs":[],"source":["# @markdown Make an archive by .zip the files in 'content/tmp'\n","import shutil\n","%cd /content/\n","shutil.make_archive('/content/images' ,'zip', '/content/tmp')\n","#----#\n","#shutil.rmtree('/content/drive/MyDrive/Saved from Chrome/tgt') #delete video\n","#from google.colab import runtime\n","#runtime.unassign() #Disconnect from runtime\n"]}],"metadata":{"colab":{"provenance":[{"file_id":"https://huggingface.co/datasets/codeShare/lora-training-data/blob/main/Video to Keyframe Extractor.ipynb","timestamp":1748371395582},{"file_id":"1G-vpB2y3uKbM4BuNg5N_1kQnb6mrxq-A","timestamp":1747499769982},{"file_id":"https://huggingface.co/datasets/codeShare/lora-training-data/blob/main/Joycaption_Alpha_One.ipynb","timestamp":1741088234107},{"file_id":"https://huggingface.co/datasets/codeShare/lora-training-data/blob/main/Joycaption_Alpha_One.ipynb","timestamp":1740768524003},{"file_id":"https://huggingface.co/datasets/codeShare/lora-training-data/blob/main/Joycaption_Alpha_One.ipynb","timestamp":1740657473013},{"file_id":"https://huggingface.co/datasets/codeShare/lora-training-data/blob/main/Joycaption_Alpha_One.ipynb","timestamp":1739796923572},{"file_id":"https://huggingface.co/datasets/codeShare/lora-training-data/blob/main/Joycaption_Alpha_One.ipynb","timestamp":1739735627072}]},"kernelspec":{"display_name":"Python 3","name":"python3"},"language_info":{"name":"python"}},"nbformat":4,"nbformat_minor":0}
 
1
+ {"cells":[{"cell_type":"code","execution_count":null,"metadata":{"id":"Dwr7gk5OwuGC"},"outputs":[],"source":["from google.colab import drive\n","drive.mount('/content/drive')"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"wTbO9mWbDXNr"},"outputs":[],"source":["\n","# @markdown 🌊 Rapid keyframe processing\n","# @markdown <br> ------ <br> Extract Keyframes from ALL mp4 / webm videos found on Google Drive\n","# @markdown <br> Be mindful of Google Drive Terms of Service\n","# @markdown <br> This cell will process all mp4 videos found under\n","# @markdown <br> /content/drive/MyDrive/Saved from Chrome/\n","delete_mp4_when_done = True # @param {type:'boolean'}\n","# @markdown <br> deleted mp4/webm files will be found under 'trash' in your Google drive\n","# @markdown <br> -------\n","# @markdown <br> (Optional) Add a direct video link to below field.\n","# @markdown <br> Multiple links can be written in this field\n","# @markdown <br> separated by comma. Like this: <br> ' https:\\\\\\my_video.mp4 , https:\\\\\\second_video.webm , .... '\n","import os\n","import shutil\n","!pip install video-kf\n","!pip install ffmpeg-python\n","!pip install wget\n","import wget\n","import videokf as vf\n","import time\n","#def mkdirs(folder):\n","# if not os.path.exists(folder):os.makedirs(folder)\n","#----#\n","direct_link = '' # @param {type:'string'}\n","# @markdown The linked videos will be downloaded to the Google drive prior to running the script.\n","# @markdown <br> This feature is useful for direct processing .webm from 4chan threads into keyframes\n","use_link = False\n","if direct_link.find('http')>-1: use_link = True\n","if use_link:\n"," %cd '/content/drive/MyDrive/Saved from Chrome/'\n"," for link in direct_link.split(','):\n"," if not link.find('http')>-1:continue\n"," wget.download(link.strip())\n"," time.sleep(5)\n"," %cd '/content/'\n","#-----#\n","filenames = []\n","srcpath = '/content/drive/MyDrive/Saved from Chrome/'\n","destpath = '/content/drive/MyDrive/'\n","localpath = '/content/'\n","converted = ''\n","for filename in os.listdir(f'{srcpath}'):\n"," for suffix in ['.mp4','.webm']:\n"," if filename.find(f'{suffix}')>-1: filenames.append(filename)\n","#Rename the downloaded video to 'tgt0' before running this cell\n","def my_mkdirs(folder):\n"," if os.path.exists(folder):shutil.rmtree(folder)\n"," os.makedirs(folder)\n","#----#\n","# @markdown Write a funny name for the folder(s) containing the keyframes\n","name_keyframes_as='' # @param {type:'string'}\n","# @markdown Created .zip files will not be overwritten\n","#NUM_ITEMS = 1 # @param {type:'slider', min:1 , max:20,step:1}\n","if name_keyframes_as.strip()=='': name_keyframes_as='keyframes'\n","num = 0\n","savepath = ''\n","for filename in filenames:\n"," tgt_folder = f'/content/tmp'\n"," my_mkdirs(f'{tgt_folder}')\n"," print(f'Now processing video {filename}...')\n"," vf.extract_keyframes(f'{srcpath}{filename}',output_dir_keyframes=f'{tgt_folder}')\n"," savepath = f'{destpath}{name_keyframes_as}_v{num}_kf'\n"," #---#\n"," while os.path.exists(f'{savepath}.zip'):\n"," #print(f'{savepath}.zip already exists...')\n"," num = num+1\n"," savepath = f'{destpath}{name_keyframes_as}_v{num}_kf'\n"," #---#\n"," shutil.make_archive(savepath,'zip' , f'{tgt_folder}')\n"," if delete_mp4_when_done: os.remove(f'{srcpath}{filename}')\n"," num = num+1\n"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Srb6DaP8-3d-"},"outputs":[],"source":["# @markdown 📮 Convert .mkv video to mp4 w/o audio at 0.5 fps <br>\n","# @markdown This script will process all .mkv files <br> found under .../Saved from Chrome/ in the drive folder\n","import moviepy.editor as moviepy\n","import os\n","import shutil\n","# @markdown higher fps to extract => longer processing time\n","# @markdown <br> Processing time at 1 fps is roughly 1:1 of the actual length of the video\n","# @markdown <br> For example; a 60 min video will take 60 min to convert to mp4 at 1 fps\n","frames_per_second = 1 # @param {type:'slider',min:0.1 , max:4 , step:0.1}\n","#src = '/content/drive/MyDrive/Saved from Chrome/ob2.mkv' # @param {type:'string'}\n","#----#\n","drivepath = '/content/drive/MyDrive/Saved from Chrome/'\n","for src in os.listdir(f'{drivepath}'):\n"," if not src.find('.mkv')>-1: continue\n"," dest = src.replace('.mkv','.mp4')\n"," if os.path.exists(f'{dest}'):continue\n"," clip = moviepy.VideoFileClip(f\"{drivepath}{src}\")\n"," clip.write_videofile(f\"{drivepath}{dest}\", audio=False, fps=0.5)\n","#----#\n"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"DlwPZ1pMyp9t"},"outputs":[],"source":["\n","# @markdown Extract Keyframes from video uploaded in '/content/'\n","import os\n","import shutil\n","!pip install video-kf\n","#Rename the downloaded video to 'tgt' before running this cell\n","def my_mkdirs(folder):\n"," if os.path.exists(folder):shutil.rmtree(folder)\n"," os.makedirs(folder)\n","#----#\n","num=0\n","tgt_folder = f'/content/tmp/'\n","my_mkdirs(f'{tgt_folder}')\n","!video-kf '/content/tgt' -dir '/content/' -o '/content/tmp/'\n","shutil.make_archive('/content/images'+f'{num}' ,'zip' , f'{tgt_folder}')\n","num = num+1\n","\n"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"hNbVdLKcGluC"},"outputs":[],"source":["# @markdown Extract Keyframes from video stored on Google Drive\n","# @markdown <br> Be mindful of Google Drive Terms of Service\n","import os\n","import shutil\n","!pip install video-kf\n","#Rename the downloaded video to 'tgt' before running this cell\n","def my_mkdirs(folder):\n"," if os.path.exists(folder):shutil.rmtree(folder)\n"," os.makedirs(folder)\n","#----#\n","num=0\n","tgt_folder = f'/content/tmp/'\n","my_mkdirs(f'{tgt_folder}')\n","!video-kf '/content/drive/MyDrive/Saved from Chrome/tgt' -dir '/content/' -o '/content/tmp/'\n","shutil.make_archive('/content/drive/MyDrive/images'+f'{num}' ,'zip' , f'{tgt_folder}')\n","num = num+1\n"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"bk4rg4zhCqZv"},"outputs":[],"source":["# @markdown .zip and upload keyframes on Google Drive\n","# @markdown <br> Be mindful of Google Drive Terms of Service\n","shutil.make_archive('/content/drive/MyDrive/images'+f'{num}' ,'zip' , f'{tgt_folder}')"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"J811UZU6xZEo"},"outputs":[],"source":["\n","# @markdown Convert all uploaded images to '/content/'\n","# @markdown <br> to the more compact .webp format\n","# @markdown and place them in the '/content/tmp' folder\n","import os\n","from PIL import Image\n","home_directory = '/content/'\n","using_Kaggle = os.environ.get('KAGGLE_URL_BASE','')\n","if using_Kaggle : home_directory = '/kaggle/working/'\n","%cd {home_directory}\n","#----#\n","def my_mkdirs(folder):\n"," if os.path.exists(folder)==False:\n"," os.makedirs(folder)\n","#----#\n","tgt_folder = f'/content/tmp/'\n","src_folder = '/content/' # @param\n","my_mkdirs(f'{tgt_folder}')\n","#---#\n","suffixes = ['.jpg','.gif', '.jpeg','.WEBP','.PNG','.png', '.JPEG' , '.webp' , '.jpg']\n","num = 1\n","for filename in os.listdir(src_folder):\n"," for suffix in suffixes:\n"," if not filename.find(suffix)>-1: continue\n"," print(filename)\n"," %cd {src_folder}\n"," input_image = Image.open(f\"{filename}\").convert('RGB')\n"," %cd {tgt_folder}\n"," input_image.save(f'{num}.webp', \"WEBP\")\n"," num = num+1"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"5EztLCjkPq4U"},"outputs":[],"source":["# @markdown Make an archive by .zip the files in 'content/tmp'\n","import shutil\n","%cd /content/\n","shutil.make_archive('/content/images' ,'zip', '/content/tmp')\n","#----#\n","#shutil.rmtree('/content/drive/MyDrive/Saved from Chrome/tgt') #delete video\n","#from google.colab import runtime\n","#runtime.unassign() #Disconnect from runtime\n"]}],"metadata":{"colab":{"provenance":[{"file_id":"https://huggingface.co/datasets/codeShare/lora-training-data/blob/main/Video to Keyframe Extractor.ipynb","timestamp":1748371395582},{"file_id":"1G-vpB2y3uKbM4BuNg5N_1kQnb6mrxq-A","timestamp":1747499769982},{"file_id":"https://huggingface.co/datasets/codeShare/lora-training-data/blob/main/Joycaption_Alpha_One.ipynb","timestamp":1741088234107},{"file_id":"https://huggingface.co/datasets/codeShare/lora-training-data/blob/main/Joycaption_Alpha_One.ipynb","timestamp":1740768524003},{"file_id":"https://huggingface.co/datasets/codeShare/lora-training-data/blob/main/Joycaption_Alpha_One.ipynb","timestamp":1740657473013},{"file_id":"https://huggingface.co/datasets/codeShare/lora-training-data/blob/main/Joycaption_Alpha_One.ipynb","timestamp":1739796923572},{"file_id":"https://huggingface.co/datasets/codeShare/lora-training-data/blob/main/Joycaption_Alpha_One.ipynb","timestamp":1739735627072}]},"kernelspec":{"display_name":"Python 3","name":"python3"},"language_info":{"name":"python"}},"nbformat":4,"nbformat_minor":0}