{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import cv2"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"----------------------- False\n"
]
}
],
"source": [
"# 視訊由一張張圖檔組成,每張圖檔叫做:幀\n",
"# 視訊,一秒24幀\n",
"# 視訊加載成功\n",
"face_detector = cv2.CascadeClassifier('./haarcascade_frontalface_alt.xml')\n",
"video = cv2.VideoCapture('./v.mp4')\n",
"# 寫視訊\n",
"fourcc = cv2.VideoWriter_fourcc('M','P','4','2')# 視訊格式\n",
"fps = 24 # 視訊幀 一秒24幀\n",
"size = (426,240) # 圖檔尺寸\n",
"write = cv2.VideoWriter('./v2.mp4',fourcc,fps,size)\n",
"while True:\n",
" flag,frame = video.read()\n",
" if flag == False:\n",
" print('-----------------------',flag)\n",
" break\n",
" frame = cv2.resize(frame,(426,240))#尺寸縮放,高度和寬度變成原來的三分之一\n",
" gray = cv2.cvtColor(frame,code = cv2.COLOR_BGR2GRAY)\n",
" face_zones = face_detector.detectMultiScale(gray)\n",
" for x,y,w,h in face_zones:\n",
"# cv2.rectangle(frame,pt1 = (x,y),pt2 = (x+w,y+h),color = [0,0,255],thickness = 2)\n",
" cv2.circle(frame,center = (x+w//2,y+h//2),radius = w//2,color = [0,0,255],thickness = 2)\n",
" cv2.imshow('ttnk',frame)\n",
" write.write(frame)\n",
" key = cv2.waitKey(41)\n",
" if key == ord('q'):#退出條件\n",
" break\n",
"cv2.destroyAllWindows()\n",
"video.release()\n",
"write.release()"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"import subprocess"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"call = 'ffmpeg -i v2.mp4 -i goon.wav out.mp4'\n",
"subprocess.call(call)"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cmd = 'ffmpeg -i \"lion my heart will go on.mp4\" -vn music3.mp3'\n",
"subprocess.call(cmd)"
]
}
],
"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.8.1"
}
},
"nbformat": 4,
"nbformat_minor": 4
}