【发布时间】:2018-07-25 16:36:04
【问题描述】:
我正在使用 raspberry pi 和 python 编码构建家庭监控,我试图通过 json 编码将一些 mp4 文件发送到 Laravel 服务器,我试图在 python 上进行 base64 编码并解码php 但是当我接收并保存它时文件似乎已损坏。所以我想知道我该怎么做,或者有更好的方法吗?
我想知道是否可能是编码文件丢失了一部分,因为我将发送的字符串与相同的字符串进行比较,但将其取回并显示为 false 等于。
如果你想在 python 上检查我的代码,我就是这样做的,我用 FFMPEG 录制视频,视频实际上可以工作,如果我用 pendrive 将视频发送到我的电脑,它也可以工作。
def record_video(self):
print('Recording')
url = 'http://127.0.0.1:8080/stream/video.mjpeg'
local_filename = url.split('/')[-1]
filename = time.strftime("%Y%m%d-%H%M%S")+'.mp4'
save_path = '/home/pi/Downloads/tesis/video'
completed_video= os.path.join(save_path, filename)
##using ffmpeg to record the video
pro = subprocess.Popen('ffmpeg -i '+url+' '+completed_video+' -y', stdout=subprocess.PIPE,
shell=True, preexec_fn=os.setsid)
time.sleep(10)
##stop the recording
os.killpg(os.getpgid(pro.pid), signal.SIGTERM)
print('Sending')
##reading the file wi rb(read byte)
with open(completed_video,'rb') as f:
##encode the video
encode_video = base64.b64encode(f.read())
##put it on the json file
json = {'ip_address': '10.10.10.110',
'date': time.strftime('%Y-%m-%d %H:%M:%S'),
'video': encode_video}
##make post request
r = self.api.post(json,'createvideo')
a = r.json()
print('send')
print(a)
path = pathlib.Path(completed_video) ##Im deleting the file after is send
path.unlink()
然后对于发布请求,我正在这样做:
def post(self,json,api):
return request(self.url+api, json, headers={'Accept': 'application/json'})
在我的用于解码 mp4 文件的 php 中,我正在这样做:
$this->validate(request(),[
'ip_address' => 'required',
'date' => 'required',
'video' => 'required'
]);
$device = Device::where('ip_address',request('ip_address'))->first();
$video_encode = request('video');
$decoded = base64_decode($video_encode);
$path = public_path().'/video/'.$device->id.'/';
$date = new \DateTime('now');
$stringdate = date_format($date, 'Y-m-d H:i:s');
$file_name = $path.str_random(8).'.mp4';
$file = fopen($file_name,'wb');
fwrite($file,$decoded);
fclose($file);
$video = Video::create([
'date' => request('date'),
'device_id' => $device->id,
'video' => $file_name
]);
return response()->json([ 'data' => $video]);
我设法创建了一个文件,但它似乎损坏了。
【问题讨论】:
标签: php python json laravel mime-types