简体   繁体   中英

Converting base64 to video

i posted file by api in js of type string as shown in picture错误图片

so i need to convert str base64 to video file when converting in python the generated file as shown in screenshot its lieke text the lesson_vid = request.POST.get('lesson_video') print(type(lesson_vid)) output is: <class 'str'> the question is how to save mp4 video from str base64

python:

lesson_title = request.POST.get('lesson_title')
            lesson_vid = request.POST.get('lesson_video')[15:]
            lesson_vid = lesson_vid.encode()

            #print(lesson_vid)
            #lesson_vid = ' '.join(format(ord(x), 'b') for x in lesson_vid)

            with open("data/video1.mp4", "wb") as fh:
                fh.write(base64.b64decode(lesson_vid))
                fh.close()
            # if len(lesson_title) > 3:
            #     pass
            # if lesson_vid != 'no-vid':
            #     pass

javaScript:

c = console.log
    const inputFile = document.querySelector('#file');
    var vid_name = document.getElementById('vid-name')
    var video_upload = 'no-vid'
    inputFile.addEventListener('click', function () {
        inputFile.click();
    })
    inputFile.addEventListener('change', function () {
        const video = this.files[0]
        const reader = new FileReader();
        reader.onload = () => {
            c(video.name)
            vid_name.innerHTML = video.name
            video_upload = reader.result
        }
        reader.readAsDataURL(video);
    })


    document.getElementById('lesson-form').addEventListener('submit', function (e) {
        e.preventDefault();

        const file = document.getElementById('file').files[0];
        console.log(file)

        const formData = new FormData();
        formData.append('lesson_video', btoa(video_upload))
        formData.append('lesson_title', 'title')
        formData.append('lesson', 'd1')
        formData.append('csrfmiddlewaretoken', '{{ csrf_token }}')


        $.post('/edit_course/{{ uid }}/', {
            'lesson_video': video_upload,
            'lesson': 'd1',
            'csrfmiddlewaretoken': '{{ csrf_token }}'
        }, function (data, status) {



        });


    })

fixed by only slicing the first indexes

lesson_vid = request.POST.get('lesson_video')[22:]

with open("data/video1.mp4", "wb") as fh:
    fh.write(base64.b64decode(lesson_vid))
    fh.close()```

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM