Created
November 5, 2019 02:43
-
-
Save ryujaehun/9b22cd5202d6044bf14495e21135a276 to your computer and use it in GitHub Desktop.
video split opencv in python
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": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import cv2\n", | |
"import matplotlib.pyplot as plt\n", | |
"import numpy as np\n", | |
"import os,glob\n", | |
"import pandas as pd\n", | |
"from pandas import DataFrame\n", | |
"from tqdm import tqdm" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"['100m over head.mp4', '100m under.mp4', 'headon.mp4']" | |
] | |
}, | |
"execution_count": 2, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"video_list=glob.glob('*.mp4')\n", | |
"video_list.sort()\n", | |
"video_list" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"['100m over head.txt', '100m under.txt', 'headon.txt']" | |
] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"meta_data=list(map(lambda x:x.strip().replace('mp4','txt'),video_list))\n", | |
"meta_data.sort()\n", | |
"meta_data" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[0.3, 0.5, 0.8, 1, 2]" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"size=[2,1,0.8,0.5,0.3]\n", | |
"size.sort()\n", | |
"size" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"videos_save_path='/video/subtitle/videos'\n", | |
"images_save_path='/video/subtitle/images'" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"\n", | |
"\n", | |
"\n", | |
"\n", | |
"0it [00:00, ?it/s]\u001b[A\u001b[A\u001b[A\u001b[A\n", | |
"\n", | |
"\n", | |
"\n", | |
"\n", | |
" 0%| | 0/5 [00:00<?, ?it/s]\u001b[A\u001b[A\u001b[A\u001b[A\u001b[A" | |
] | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"/home/jaehun/video/subtitle/videos/0.3/100m over head.mp4\n" | |
] | |
}, | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"\n", | |
"\n", | |
"\n", | |
"\n", | |
"\n", | |
" 20%|██ | 1/5 [00:15<01:02, 15.55s/it]\u001b[A\u001b[A\u001b[A\u001b[A\u001b[A" | |
] | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"/home/jaehun/video/subtitle/videos/0.5/100m over head.mp4\n" | |
] | |
}, | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"\n", | |
"\n", | |
"\n", | |
"\n", | |
"\n", | |
" 40%|████ | 2/5 [00:37<00:52, 17.55s/it]\u001b[A\u001b[A\u001b[A\u001b[A\u001b[A" | |
] | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"/home/jaehun/video/subtitle/videos/0.8/100m over head.mp4\n" | |
] | |
}, | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"\n", | |
"\n", | |
"\n", | |
"\n", | |
"\n", | |
" 60%|██████ | 3/5 [01:12<00:45, 22.57s/it]\u001b[A\u001b[A\u001b[A\u001b[A\u001b[A" | |
] | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"/home/jaehun/video/subtitle/videos/1/100m over head.mp4\n" | |
] | |
}, | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"\n", | |
"\n", | |
"\n", | |
"\n", | |
"\n", | |
" 80%|████████ | 4/5 [01:52<00:27, 27.91s/it]\u001b[A\u001b[A\u001b[A\u001b[A\u001b[A" | |
] | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"/home/jaehun/video/subtitle/videos/2/100m over head.mp4\n" | |
] | |
} | |
], | |
"source": [ | |
"\n", | |
"for video,txt in tqdm(zip(video_list,meta_data)):\n", | |
" for j in tqdm(size):\n", | |
" # txt 파일 읽기\n", | |
" df=pd.read_csv(txt,names=['times','r'],sep=\" \", header=None)\n", | |
" # video 파일 읽기\n", | |
" cap = cv2.VideoCapture(video)\n", | |
" # 저장할 video의 크기를 정해준다.\n", | |
" width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)*j)\n", | |
" height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)*j)\n", | |
" # 아래는 동영상과 txt데이터를 보간하기 위한 로직\n", | |
" n_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))\n", | |
" while df.index.stop!=n_frames:\n", | |
" df=df.append(pd.Series(), ignore_index=True)\n", | |
" df['times']=df.index+1\n", | |
" df['r'].interpolate(method ='linear',inplace=True) \n", | |
" # 기존의 video에서 fps 정보를 가져온다\n", | |
" fps = cap.get(cv2.CAP_PROP_FPS)\n", | |
" # 코덱을 정한다.\n", | |
" fourcc = cv2.VideoWriter_fourcc(*'XVID')\n", | |
" # 저장할 이름을 정한다. \n", | |
" outname='/home/jaehun'+videos_save_path+'/'+str(j)+'/'+video\n", | |
" os.makedirs('/home/jaehun'+videos_save_path+'/'+str(j),exist_ok=True)\n", | |
" os.makedirs('/home/jaehun'+images_save_path+'/'+video.split('.')[0]+'/'+str(j),exist_ok=True)\n", | |
" print(outname)\n", | |
" out = cv2.VideoWriter(outname, fourcc, fps, (width,height))\n", | |
" count=0\n", | |
" time=''\n", | |
" r=''\n", | |
" while(cap.isOpened()):\n", | |
" ret,frame = cap.read()\n", | |
" if ret:\n", | |
" time=str(df['times'][count])\n", | |
" r=str(df['r'][count])\n", | |
" # 크기를 조정한다.\n", | |
" frame=cv2.resize(frame,tuple(map (lambda x:(int)(x*j),frame.shape[:2][::-1])),interpolation=cv2.INTER_CUBIC )\n", | |
" #str1 시간 정보를 삽입한다.\n", | |
" cv2.putText(frame,\"time: \"+ time, ((int)(250*j), height-(int)(250*j)), cv2.FONT_HERSHEY_PLAIN, (int)(5*j), (0, 0 ,255))\n", | |
" #str2 거리 정보를 삽입한다.\n", | |
" cv2.putText(frame, \"r: \"+ r, (width-(int)(450*j), height-(int)(250*j)), cv2.FONT_HERSHEY_PLAIN, (int)(5*j), (0, 0, 255))\n", | |
" out.write(frame)\n", | |
" cv2.imwrite('/home/jaehun'+images_save_path+'/'+video.split('.')[0]+'/'+str(j)+\"/%d.png\" % count, frame) # save frame as JPEG file \n", | |
" else:\n", | |
" break\n", | |
" count+=1\n", | |
" cap.release()\n", | |
" out.release()\n", | |
" cv2.destroyAllWindows()\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"videos_save_path='/video/split/videos'\n", | |
"images_save_path='/video/split/images'" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"\n", | |
"for video,txt in tqdm(zip(video_list,meta_data)):\n", | |
" for j in tqdm(size):\n", | |
" # txt 파일 읽기\n", | |
" # video 파일 읽기\n", | |
" cap = cv2.VideoCapture(video)\n", | |
" # 저장할 video의 크기를 정해준다.\n", | |
" width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)*j)\n", | |
" height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)*j)\n", | |
" \n", | |
" # 기존의 video에서 fps 정보를 가져온다\n", | |
" fps = cap.get(cv2.CAP_PROP_FPS)\n", | |
" # 코덱을 정한다.\n", | |
" fourcc = cv2.VideoWriter_fourcc(*'XVID')\n", | |
" # 저장할 이름을 정한다. \n", | |
" outname='/home/jaehun'+videos_save_path+'/'+str(j)+'/'+video\n", | |
" os.makedirs('/home/jaehun'+videos_save_path+'/'+str(j),exist_ok=True)\n", | |
" os.makedirs('/home/jaehun'+images_save_path+'/'+video.split('.')[0]+'/'+str(j),exist_ok=True)\n", | |
" print(outname)\n", | |
" out = cv2.VideoWriter(outname, fourcc, fps, (width,height))\n", | |
" count=0\n", | |
" while(cap.isOpened()):\n", | |
" ret,frame = cap.read()\n", | |
" if ret:\n", | |
"\n", | |
" # 크기를 조정한다.\n", | |
" frame=cv2.resize(frame,tuple(map (lambda x:(int)(x*j),frame.shape[:2][::-1])),interpolation=cv2.INTER_CUBIC )\n", | |
" out.write(frame)\n", | |
" cv2.imwrite('/home/jaehun'+images_save_path+'/'+video.split('.')[0]+'/'+str(j)+\"/%d.png\" % count, frame) # save frame as JPEG file \n", | |
" else:\n", | |
" break\n", | |
" count+=1\n", | |
" cap.release()\n", | |
" out.release()\n", | |
" cv2.destroyAllWindows()\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"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.6.8" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment