用ffmpeg进行视频处理

Bertha 。 2022-11-24 15:28 478阅读 0赞

1.下载及安装

在ffmpeg官网https://ffmpeg.zeranoe.com/builds/可以下载到需要的版本,然后解压到D盘,添加环境变量(如D:\\ffmpeg\\bin)
在这里插入图片描述
在cmd输入ffmpeg,出现如图现象,即为安装成功

2.使用

  1. #视频处理
  2. def file_name(path_file):
  3. for i in range(len(path_file)):#找到不含地址的文件名
  4. if(path_file[-i-1]=='\\'):
  5. name = path_file[-i:]
  6. break
  7. return name
  8. class video():
  9. def __init__(self):
  10. print('(1)视频合并\n(2)视频剪辑\n(3)视频格式转换\n(4)提取音频')
  11. print('(5)视频拆分成帧图片\n(6)帧图片合成视频')
  12. b = eval(input('请选择:\n'))
  13. if(b==1):
  14. self.video_convert()
  15. elif(b==2):
  16. self.video_clip()
  17. elif(b==3):
  18. self.video_format_conversion()
  19. elif(b==4):
  20. print('请输入视频的存储路径:')
  21. video = input(r'')
  22. cmd = 'ffmpeg -i {} -f mp3 -vn output.mp3'.format(video)
  23. subprocess.call(cmd,shell=True)
  24. print('提取完成!')
  25. elif(b==5):
  26. self.video_frame_photo()
  27. elif(b==6):
  28. self.frame_photo_video()
  29. else:
  30. print('输入有误!')
  31. #视频合并
  32. def video_convert(self):
  33. print('请输入视频个数:')
  34. num = eval(input())
  35. filelist = open('filelist.txt','a')
  36. for i in range(num):
  37. print('请输入视频%d的存储路径:'%(i+1))
  38. a = input(r'')
  39. filelist.write("file '"+ a +"'\n")
  40. filelist.close()
  41. cmd = 'ffmpeg -f concat -safe 0 -i filelist.txt -c copy output.mp4'
  42. subprocess.call(cmd,shell=True)
  43. print('合并完成!')
  44. #视频格式转换
  45. def time(self,file_path):
  46. cap = cv2.VideoCapture(file_path)
  47. # get方法参数按顺序对应下表(从0开始编号)
  48. rate = cap.get(5) # 帧速率
  49. FrameNumber = cap.get(7) # 视频文件的帧数
  50. duration = FrameNumber/rate/60 # 帧速率/视频总帧数 是时间,除以60之后单位是分钟
  51. minutes = int(duration)
  52. seconds = int((duration - minutes) * 60)
  53. return str(minutes) + ':' + str(seconds)
  54. def video_format_conversion(self):
  55. while True:
  56. print('请输入视频路径:')
  57. video = input()
  58. #print('请输入需要转换的视频格式:')
  59. style = 'mp4'#input()
  60. time = self.time(video)
  61. cmd = 'ffmpeg -i {} -ss 00:00:00 -c copy -to {} {}.{}'.format(video,time,file_name(video),style)
  62. subprocess.call(cmd,shell=True)
  63. print('转换完成!')
  64. #视频剪辑
  65. def video_clip(self):
  66. print('请输入视频路径:')
  67. video = input()
  68. print('请输入开始时间:')
  69. start = input()
  70. print('请输入结束时间:')
  71. end = input()
  72. cmd = 'ffmpeg -i {} -ss {} -c copy -to {} output.mp4'.format(video,start,end)
  73. subprocess.call(cmd,shell=True)
  74. print('剪辑完成!')
  75. #视频拆分成帧图片
  76. def video_frame_photo(self):
  77. print('请输入视频的存放路径:')
  78. videos_path = input(r'') #视频的存放路径
  79. print('请输入帧图片的存放路径:')
  80. frames_save_path = input(r'') #视频切分成帧之后图片的保存路径
  81. print('每几帧中保存1帧?')
  82. time_interval = eval(input()) #每1帧保存一次
  83. vidcap = cv2.VideoCapture(videos_path)
  84. success, image = vidcap.read()
  85. count = 0
  86. while success:
  87. success, image = vidcap.read()
  88. count = count + 1
  89. if count % time_interval == 0:
  90. cv2.imencode('.jpg', image)[1].tofile(frames_save_path + "frame%d.jpg" % count)
  91. print('拆分完成!')
  92. #帧图片合成视频
  93. def frame_photo_video(self):
  94. print('请输入帧图片存放路径:')
  95. im_dir = input(r'')#帧存放路径
  96. print('请输入合成视频的存放路径:')
  97. video_dir = input(r'') #合成视频存放的路径
  98. print('请输入帧率:')
  99. fps = eval(input()) #帧率
  100. im_list = os.listdir(im_dir)
  101. im_list.sort(key=lambda x: int(x.replace("frame","").split('.')[0]))
  102. img = Image.open(os.path.join(im_dir,im_list[0]))
  103. img_size = img.size #获得图片分辨率,im_dir文件夹下的图片分辨率需要一致
  104. fourcc = cv2.VideoWriter_fourcc(*'XVID')
  105. videoWriter = cv2.VideoWriter(video_dir, fourcc, fps, img_size)
  106. for i in im_list:
  107. im_name = os.path.join(im_dir+i)
  108. frame = cv2.imdecode(np.fromfile(im_name, dtype=np.uint8), -1)
  109. videoWriter.write(frame)
  110. videoWriter.release()
  111. print('合成完成!')

此外,还有一些其他功能:

4.其它功能

提取无声视频

  1. ffmpeg -i video.mp4 -i audio.wav -c:v copy -c:a aac -strict experimental output.mp4

合并两个音频

  1. ffmpeg -i input1.mp3 -i input2.mp3 -filter_complex amerge -ac 2 -c:a libmp3lame -q:a 4 output.mp3

提取字幕:

  1. ffmpeg -i input.mkv -vn -an -codec:s:0 srt subtitle.srt

添加字幕:

  1. ffmpeg -i input.mkv -vf subtitles=subtitle.srt output.mp4

音视频合并:

  1. ffmpeg -i video.mp4 -i audio.wav -c:v copy -c:a aac -strict experimental output.mp4

发表评论

表情:
评论列表 (有 0 条评论,478人围观)

还没有评论,来说两句吧...

相关阅读