【问题标题】:Python to Laravel Upload mp4 FilePython 到 Laravel 上传 mp4 文件
【发布时间】: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


    【解决方案1】:

    我希望 this stackoverflow post 了解如何使用请求帖子发送文件: file_ = {'file': ('video.mp4', open('video.mp4', 'rb'))} r = requests.post(upload_url, files=file_)

    然后我会向the laravel documentation 了解如何管理上传文件的存储: $path = request()->file->store('images');

    您应该能够进行文件验证,并且无需对数据进行基础编码。希望将open() 传递给file_requests 处理文件。

    【讨论】:

    • 我尝试按照您所说的方式发送它,但是当我验证它时说它不是 mp4 文件 $this->validate(request(),[ 'file' => 'required|mimes: mp4' ]); 所以我猜我没有发送 mp4 文件我没有知道发送了什么
    猜你喜欢
    • 1970-01-01
    • 2017-08-02
    • 2023-03-14
    • 1970-01-01
    • 2021-07-18
    • 1970-01-01
    • 2018-06-11
    • 2014-11-08
    • 1970-01-01
    相关资源
    最近更新 更多