Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert video file from ios to base64 in ionic 3

I am trying to convert a video url which we can retrieve from an iOS device and convert it to BASE64 in ionic 3, but I am unable to achieve BASE64 url.

iOS Video URL: filePath = /var/mobile/Containers/Data/Application/3436A7EB-4684-4618-8125-3E6AE1645FCE/Documents/MUS_RA/1534429730643_capturedvideo.MOV

I tried the following code to convert the video URL to BASE64 by using the BASE64 cordova plugin but no luck

this.base64.encodeFile(filePath)
            .then((base64String: string) => {
                 console.log("base64VideoChange");
                resolve(base64String);
             }, (err) => {
                 console.log("base64VideoNOTChange");
                 reject(err);
             });

Note: I am using ionic 3. Please help.

like image 324
Priyank Avatar asked Aug 17 '18 06:08

Priyank


2 Answers

Finally i resolved my issue by using ionic readAsDataURL:

Imports File plugin to app.module.ts

import { File, DirectoryEntry, FileEntry } from '@ionic-native/file';

In page.ts

import { File, DirectoryEntry, FileEntry } from '@ionic-native/file';
getBase64StringByFilePath(fileURL): Promise<string> {
    return new Promise((resolve, reject) => {

        let fileName = fileURL.substring(fileURL.lastIndexOf('/') + 1);
        let filePath = fileURL.substring(0, fileURL.lastIndexOf("/") + 1);
        this.file.readAsDataURL(filePath, fileName).then(
            file64 => {
                console.log(file64); //base64url...
                resolve(file64);
            }).catch(err => {
                reject(err);
          });
    })
}
like image 96
Priyank Avatar answered Oct 05 '22 02:10

Priyank


Imports File plugin to app.module.ts .

import { Base64 } from '@ionic-native/base64';

...
providers: [
    ...
    Base64
  ]

in Page:

import { Base64 } from '@ionic-native/base64';

constructor(private base64: Base64) { }


let filePath: string = 'file:///...';
this.base64.encodeFile(filePath).then((base64File: string) => {
  console.log(base64File);
}, (err) => {
  console.log(err);
});

Or use it thins plugins

like image 29
Gurbela Avatar answered Oct 05 '22 02:10

Gurbela