from flask import Flask, request
import cv2
import datetime
import subprocess
import time
app = Flask(__name__)
@
app.route('/capture_video', methods=['POST'])
def capture_video():
try:
# 获取当前时间
current_time = datetime.datetime.now()
start_time = (current_time - datetime.timedelta(seconds=5)).strftime('%H:%M:%S')
end_time = (current_time + datetime.timedelta(seconds=5)).strftime('%H:%M:%S')
# 摄像头 ID ,假设使用默认摄像头
camera_id = 0
# 保存视频的路径
output_path = 'captured_video.mp4'
# 使用 FFmpeg 捕获视频
ffmpeg_cmd = f'ffmpeg -y -ss {start_time} -t 10 -i /dev/video{camera_id} {output_path}'
subprocess.run(ffmpeg_cmd, shell=True, check=True)
# 推送视频到指定地方(这里假设推送到某个 URL )
push_url = '
http://example.com/upload' push_cmd = f'curl -F "file=@{output_path}" {push_url}'
subprocess.run(push_cmd, shell=True, check=True)
return {'status': 'success', 'message': 'Video captured and pushed successfully'}
except Exception as e:
return {'status': 'error', 'message': str(e)}
if __name__ == '__main__':
app.run(debug=True)