目錄
前言
環境部署
代碼
總結
前言
獲取MP4視頻第一幀圖片,mp4的視頻路徑支持url鏈接,
環境部署
需要部署ffmpeg環境,具體的部署步驟可以參考我的另一篇文章:windows ffmpeg安裝部署_阿良的博客-CSDN博客
代碼
不廢話,上代碼,
#!/user/bin/env python
# coding=utf-8
"""
@project : csdn
@author : huyi
@file : extract_video_first_frame.py
@ide : PyCharm
@time : 2021-11-11 21:33:38
"""
import os
import subprocess
import uuid
import urllib.request
def extract(video_path: str, tmp_dir: str):
if video_path.startswith("http"):
mp4_path = _download_mp4(video_path, tmp_dir)
else:
mp4_path = video_path
pic_path = os.path.join(tmp_dir, '{}.jpg'.format(uuid.uuid4()))
ffmpeg_cmd = 'ffmpeg -i {} -f image2 -ss 1 -frames:v 1 {}'.format(
mp4_path, pic_path)
print(ffmpeg_cmd)
ffmpeg_pipe = subprocess.Popen(ffmpeg_cmd, shell=True)
ffmpeg_pipe.wait()
return pic_path
def _download_mp4(video_url: str, tmp_dir: str):
new_video_path = os.path.join(tmp_dir, '{}.mp4'.format(uuid.uuid4()))
urllib.request.urlretrieve(video_url, new_video_path)
return new_video_path
驗證一下,驗證代碼如下
if __name__ == '__main__':
print(
extract(
'http://xxxxxxx/test1.mp4',
'C:/Users/huyi/Desktop'))

總結
沒啥好總結的,java我也做了個工具類,給自己做個記憶點,
分享:
遇見你之前,不知道什么叫喜歡,錯過你之后,不知道什么叫喜歡,——《雪中悍刀行》
如果本文對你有幫助的話,請不要吝嗇你的贊,謝謝!

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/356125.html
標籤:python
上一篇:使用Python控制手機(一)
