由於語法渲染問題而影響閱讀體驗, 請移步博客閱讀~
本文GitPage地址

VedioSlice.py

  1. ##!/usr/bin/env python3
  2. ## -*- coding: utf-8 -*-
  3. ## @Time : 2020/4/18
  4. ## @Author : Karobben
  5. ## @Site : China
  6. ## @File : VedioSlice.py
  7. ## @Software: Atom
  8. import argparse
  9. parser = argparse.ArgumentParser()
  10. parser.add_argument('-i','-I','--input',
  11. help='Input vedio file') #输入文件
  12. parser.add_argument('-o','-U','--output', default = "out_test.avi",
  13. help='Output vedio file, default as "out_test.avi"') #输入文件
  14. parser.add_argument('-s','-S','--Start', type = int, default = 0,
  15. help='Start from X second. default from 0') #输入文件
  16. parser.add_argument('-e','-E','--End', type = int, default = 1,
  17. help='End at X second, defalt at 1s') #输入文件
  18. ##获取参数
  19. args = parser.parse_args()
  20. INPUT = args.input
  21. OUTPUT = args.output
  22. Slice_S = args.Start
  23. Slice_E = args.End
  24. import cv2
  25. import numpy as np
  26. ##INPUT = 'bug.avi'
  27. cap = cv2.VideoCapture(INPUT)
  28. fps_c = cap.get(cv2.CAP_PROP_FPS)
  29. Vedio_h = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
  30. Vedio_w = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
  31. print("Current fps:",fps_c)
  32. Slice_S = Slice_S* fps_c
  33. Slice_E = Slice_E* fps_c
  34. ##OUTPUT = "out_test.avi"
  35. fps_o = fps_c
  36. Out_size = (int(Vedio_w),int(Vedio_h))
  37. fourcc = cv2.VideoWriter_fourcc('M','J','P','G')
  38. videowriter = cv2.VideoWriter(OUTPUT,fourcc,fps_o,Out_size)
  39. Num = 0
  40. while (True):
  41. Num += 1
  42. ret,frame=cap.read()
  43. if Num >= Slice_S and Num <= Slice_E:
  44. videowriter.write(frame)
  45. if Num >= Slice_E:
  46. break
  47. videowriter.release()

How to use it

  1. ## print the vedio from 0s to 1s to out_test.avi
  2. Vedio_slice.py -i bug.avi
  3. ## print the vedio from 10s to 20s to sliced.avi file
  4. Vedio_slice.py -i bug.avi -s 10 -e 20 -o sliced.avi

Enjoy~

本文由Python腳本GitHub/語雀自動更新

由於語法渲染問題而影響閱讀體驗, 請移步博客閱讀~
本文GitPage地址

GitHub: Karobben
Blog:Karobben
BiliBili:史上最不正經的生物狗