0

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 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

i tried to send it the way you said but when i'm validating it says it's not a mp4 file $this->validate(request(),[ 'file' => 'required|mimes: mp4' ]); so my guess is that i'm not sending a mp4 file i don't know what is sending

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.