Python到Laravel上传mp4文件 [英] Python to Laravel Upload mp4 File
问题描述
我正在用覆盆子pi构建家庭监控并以python编码,并且我尝试通过json编码将一些mp4文件发送到Laravel服务器,我尝试在python上进行base64编码并在其中解码PHP的,但似乎文件已损坏,当我重新接收并保存它.所以我想知道如何才能做到这一点,或者有更好的方法做到这一点?
I'm building a home surveillance with a raspberry pi and coding in python, and I'm trying to send some mp4 files to a Laravel server via json encoded, I tried to make a base64 encoding on the python and decoded in php but it seems that the file is broken when i recive it and save it. So I'm wondering how could I do this or is there a better way to do this?
我想知道是否可能是编码文件缺少一部分,因为我正在比较发送的字符串与相同的字符串,但将其取回,并且显示为假,等于.
I'm wondering if it could be that the encoded file there's a part missing because i'm comparing the string i send vs the same string but getting it back and it show is false that are equals.
如果您想在python上检查我的代码,这就是我的操作方式,那么我正在使用FFMPEG录制视频,视频实际上可以正常工作,如果我将笔式驱动器的视频发送到计算机上也可以正常工作.
If you wanna check my code on the python this is how i do it I'm recording the video with FFMPEG the video actually works and if i send the video with a pendrive to my computer it works too.
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()
然后针对发布请求,我正在这样做:
Then for the post request i'm doing this:
def post(self,json,api):
return request(self.url+api, json, headers={'Accept': 'application/json'})
在我的用于解码mp4文件的php中,我正在这样做:
And in my php for decoding the mp4 file I'm doing this:
$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]);
我设法创建了一个文件,但它似乎坏了.
I manage to create a file but it seems broken.
推荐答案
I'd look to this stackoverflow post on how to send a file using request post:
file_ = {'file': ('video.mp4', open('video.mp4', 'rb'))}
r = requests.post(upload_url, files=file_)
然后,我会 laravel文档如何管理存储上传的文件:
$path = request()->file->store('images');
And then I'd look to the laravel documentation for how to manage storing uploaded files:
$path = request()->file->store('images');
您应该能够进行文件验证,并且不需要对数据进行基本编码.希望将open()
传递给file_
可以让requests
处理文件.
You should be able to do file validation and there's no need to base encode the data. Hopefully passing the open()
to the file_
lets the requests
handle the file.
这篇关于Python到Laravel上传mp4文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!