douzhushen_9776
2018-02-15 00:33
浏览 53

Python到Laravel上传mp4文件

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.

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'})

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.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dougu5886 2018-02-15 04:15
    已采纳

    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_)

    And then I'd look to the laravel documentation for how to manage storing uploaded files: $path = request()->file->store('images');

    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.

    打赏 评论

相关推荐 更多相似问题