1.视频处理—-人脸识别

  1. import numpy as np
  2. import cv2
  3. import matplotlib.pyplot as plt
  4. cap = cv2.VideoCapture("images/2.mp4")
  5. fourcc = cv2.VideoWriter_fourcc(*'XVID')
  6. out = cv2.VideoWriter("1.avi", fourcc, 20, (544, 960))
  7. while cap.isOpened():
  8. ret, imgs = cap.read()
  9. urlXml = 'D:/Users/24268/anaconda3/envs/python_keras/Lib/site-packages/cv2/data/'
  10. # img = cv2.imread(imgs)
  11. faceCascade = cv2.CascadeClassifier(urlXml + 'haarcascade_frontalface_default.xml')
  12. if ret is True:
  13. gray = cv2.cvtColor(imgs, cv2.COLOR_BGR2GRAY)
  14. faces = faceCascade.detectMultiScale(
  15. gray, scaleFactor=1.15, minNeighbors=5, minSize=(5, 5)
  16. )
  17. # print(faces)
  18. print(len(faces))
  19. for (x, y, w, h) in faces:
  20. cv2.circle(imgs, (int((x + x + w) / 2), int((y + y + h) / 2)), int(w / 2), (0, 255, 0), 2)
  21. cv2.imshow("renlian", imgs)
  22. cv2.waitKey(1)
  23. out.write(imgs)
  24. cap.release()
  25. out.release()

2.cv2处理的视频是avi,转为mp4

  1. import os
  2. ffmpeg = r'C:\Users\24268\Desktop\ffmpeg-N-102809-gde8e6e67e7-win64-gpl-shared\ffmpeg-N-102809-gde8e6e67e7-win64-gpl-shared\bin\ffmpeg.exe' # 写自己的安装路径
  3. # 由于路径中有空格,所以路径需要用上双引号,否则会找不到该文件
  4. cmd = ffmpeg + " -i " + '1.avi' + " -c copy " + '2.mp4'
  5. os.system(cmd)
  6. print("done")

3.cv2处理的视频不带声音,所以

  1. from moviepy.editor import *
  2. #需添加背景音乐的视频
  3. video_clip = VideoFileClip(r'F:\test\video\aaa\moviepy中文手册\素材\video.mp4')
  4. #背景音乐
  5. audio_clip = AudioFileClip(r'F:\test\video\aaa\moviepy中文手册\素材\音频\察觉.mp3')
  6. #视频写入背景音
  7. final_video = video_clip.set_audio(audio_clip)
  8. #将处理完成的视频保存
  9. final_video.write_videofile("video_result.mp4")