87 lines
2.9 KiB
Python
87 lines
2.9 KiB
Python
|
#!coding=utf-8
|
|||
|
import socket
|
|||
|
import struct
|
|||
|
import os
|
|||
|
import time
|
|||
|
import sys
|
|||
|
|
|||
|
FFMPEG_EXE = os.path.join(sys.path[0], 'LSZXVideo\\ffmpeg.exe')
|
|||
|
# os.popen(FFMPEG_EXE)
|
|||
|
input_dir = os.path.join(sys.path[0], "LSZXVideo\\Video")
|
|||
|
output_dir = os.path.join(sys.path[0], "LSZXVideo\\Output_Video")
|
|||
|
# 如果没有Output_Video这个文件夹,则创建这个文件夹
|
|||
|
if not os.path.exists(output_dir):
|
|||
|
os.mkdir(output_dir)
|
|||
|
|
|||
|
|
|||
|
class VideoSending(object):
|
|||
|
def __init__(self):
|
|||
|
self.input_video_path = input_dir
|
|||
|
self.new_resolution = "640x400"
|
|||
|
self.new_fps = "10"
|
|||
|
self.output_video_path = output_dir
|
|||
|
self.filename = None
|
|||
|
|
|||
|
def convert_video(self, video_path):
|
|||
|
video_name, _ = os.path.splitext(video_path)
|
|||
|
input_video_path = self.input_video_path + os.sep + video_path
|
|||
|
output_video_path = self.output_video_path + os.sep + video_name + ".mp4"
|
|||
|
ffmpeg_command = (f"{FFMPEG_EXE} -i %s -s %s -r %s %s" % (
|
|||
|
input_video_path, self.new_resolution, self.new_fps, output_video_path))
|
|||
|
os.system(ffmpeg_command)
|
|||
|
|
|||
|
def video_sending(self, ip, videoname, port=8025):
|
|||
|
# 创建套接字
|
|||
|
ADDR = (ip, port)
|
|||
|
ip_port = ADDR
|
|||
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|||
|
while True:
|
|||
|
try:
|
|||
|
s.connect(ip_port) # 建立连接
|
|||
|
if s:
|
|||
|
break
|
|||
|
except socket.error as e:
|
|||
|
print('Socket recv data error:', e)
|
|||
|
time.sleep(0.5)
|
|||
|
# print("成功连接服务器")
|
|||
|
|
|||
|
self.filename = os.path.join(self.output_video_path, videoname)
|
|||
|
# fileinfo_size = struct.calcsize('128sl')
|
|||
|
fhead = struct.pack('128sl', videoname.encode('gbk'), os.stat(self.filename).st_size)
|
|||
|
s.send(fhead) # 发送文件名称和大小
|
|||
|
# 发送视频
|
|||
|
f = open(self.filename, 'rb')
|
|||
|
while True:
|
|||
|
try:
|
|||
|
data = f.read()
|
|||
|
if not data:
|
|||
|
# print('{0} 数据传输完成...'.format(filename))
|
|||
|
break
|
|||
|
s.send(data)
|
|||
|
|
|||
|
except socket.error as e:
|
|||
|
print('Socket recv data error:', e)
|
|||
|
s.connect(ip_port)
|
|||
|
s.recv(1024)
|
|||
|
# print("所有数据传送完成...")
|
|||
|
|
|||
|
# 断开
|
|||
|
|
|||
|
def delete_video(self):
|
|||
|
for root, dirs, files in os.walk(self.output_video_path):
|
|||
|
for name in files:
|
|||
|
if name.endswith(".mp4"):
|
|||
|
os.remove(os.path.join(root, name))
|
|||
|
|
|||
|
|
|||
|
if __name__ == '__main__':
|
|||
|
a = VideoSending()
|
|||
|
input_video_list = os.listdir('Video')
|
|||
|
for video_path in input_video_list:
|
|||
|
print(video_path)
|
|||
|
# a.convert_video(vide_path)
|
|||
|
# output_video_list = os.listdir('./Output_Video')
|
|||
|
# for filename in output_video_list:
|
|||
|
# a.video_sending(ip='192.168.1.28', videoname=filename)
|
|||
|
# a.delete_video()
|