2018-02-15 00:33
浏览 53


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):
    url = ''
    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)
    ##stop the recording
    os.killpg(os.getpgid(, signal.SIGTERM)


    ##reading the file wi rb(read byte)
    with open(completed_video,'rb') as f:

        ##encode the video
        encode_video = base64.b64encode(

        ##put it on the json file
        json = {'ip_address': '',
                'date': time.strftime('%Y-%m-%d %H:%M:%S'),
                'video': encode_video}

        ##make post request
        r =,'createvideo')
        a = r.json()
        path = pathlib.Path(completed_video) ##Im deleting the file after is send

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:

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

    $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 =, 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.

    打赏 评论

相关推荐 更多相似问题