I have a function to select video from media gallery, then compress it, and upload it to server. To compress the video i use cordova-plugin-video-editor, the problem is i need to cut the video duration from 0-10 seconds if the video is longer than 10 seconds, but i don’t know how to do it.
let vidOptions: CameraOptions = {
quality: 80,
destinationType: this.camera.DestinationType.FILE_URI,
sourceType: this.camera.PictureSourceType.PHOTOLIBRARY,
mediaType: this.camera.MediaType.VIDEO
}
this.cameraIsCaptureVideo(vidOptions)
cameraIsCaptureVideo(options: CameraOptions){
this.camera.getPicture(options).then(video => {
this.file.resolveLocalFilesystemUrl(video).then(fileEntry => {
this.compressVideo(fileEntry.fullPath)
})
}, (err) => {
console.log(err)
this.loading.dismiss()
});
}
async compressVideo(path){
const fileName = 'story-' + moment().format('YYYY-MM-DD-HH-mm-ss');
this.videoEditor.transcodeVideo({
fileUri: path,
outputFileName: fileName,
outputFileType: this.videoEditor.OutputFileType.MPEG4,
saveToLibrary: false,
optimizeForNetworkUse: this.videoEditor.OptimizeForNetworkUse.YES,
maintainAspectRatio: true,
width: 320,
height: 640,
videoBitrate: 1000000, // 1 megabit
audioChannels: 2,
audioSampleRate: 44100,
audioBitrate: 128000, // 128 kilobits
})
.then((fileUri: string) => {
let options: FileUploadOptions = {
fileKey: 'file',
fileName: fileName+'.mp4',
mimeType: 'video/mp4',
chunkedMode: false,
headers: {
Connection: "close"
}
}
this.services.uploadFile(fileUri, options)
.then(file => {
this.tabs.select(0)
const story = {type: 'video'}
this.home.push(StoryFormPage, {file, story})
}).catch(error => {
console.log(error)
}).then( () => this.loading.dismiss() )
})
.catch((error: any) => console.log('video transcode error', error));
}