GBase 8a之视频数据存取demo
📅 2026/7/2 11:52:58
👁️ 阅读次数
📝 编程学习
概述
在GBase8a数据库(gbase database)中视频文件如何导入,以及如何读取视频文件并播放。
实现步骤
(1)建表
CREATE TABLE "vtest" ( "v_name" varchar(50) DEFAULT NULL, "v_data" longblob ) ; 注意: longblob类型,目前最大支持64MB的存储空间(2)视频文件数据导入数据库
gbase> INSERT INTO vtest (v_name, v_data) VALUES ('1.mp4', LOAD_FILE('/home/gbase/1.mp4') ); Query OK, 1 row affected (Elapsed: 00:00:00.21)(3)从数据库中读取视频文件并播放
def read_video_from_mysql(host, user, password, db, table, video_name, port): try: conn = pymysql.connect( host=host, user=user, password=******, database=db, port=port, charset='utf8mb4' ) cursor = conn.cursor() sql = f"SELECT v_data FROM {table} WHERE v_name = %s" cursor.execute(sql, (video_name,)) result = cursor.fetchone() if not result or not result[0]: print("未找到对应的视频数据!") return video_blob = result[0] with tempfile.NamedTemporaryFile(suffix='.mp4', delete=False) as temp_file: temp_file.write(video_blob) temp_file_path = temp_file.name cap = cv2.VideoCapture(temp_file_path) if not cap.isOpened(): print("无法打开视频文件!") os.unlink(temp_file_path) return while cap.isOpened(): ret, frame = cap.read() if ret: cv2.imshow('Video from GBASE', frame) if cv2.waitKey(25) & 0xFF == ord('q'): break else: break cap.release() cv2.destroyAllWindows() os.unlink(temp_file_path) except pymysql.MySQLError as e: print(f"数据库操作错误:{e}") except Exception as e: print(f"程序执行错误:{e}") finally: if 'cursor' in locals(): cursor.close() if 'conn' in locals(): conn.close() if __name__ == "__main__": read_video_from_mysql( host="192.168.56.101", user="gbase", password="******", db="vc_a.test", table="vtest", video_name="1.mp4", port=5258 )(4)验证执行播放效果
视频信息可以正常播放
编程学习
技术分享
实战经验