mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-19 04:57:52 +00:00
124 lines
3.3 KiB
TypeScript
124 lines
3.3 KiB
TypeScript
import { Injectable } from '@angular/core';
|
|
import { HttpClient } from '@angular/common/http';
|
|
import { Filesystem, FilesystemDirectory } from '@capacitor/filesystem';
|
|
import { Platform } from '@ionic/angular';
|
|
|
|
declare var FFmpeg: any;
|
|
|
|
@Injectable({
|
|
providedIn: 'root'
|
|
})
|
|
export class VideoCompressionService {
|
|
|
|
constructor(
|
|
private http: HttpClient,
|
|
private platform: Platform
|
|
) { }
|
|
|
|
async compressVideo(inputFile: string, outputFileName: string, quality: string = 'medium'): Promise<void> {
|
|
// Load FFmpeg
|
|
await this.loadFFmpeg();
|
|
|
|
// Input file
|
|
const inputBuffer = await this.fetchVideo(inputFile);
|
|
|
|
// Output file path
|
|
const outputPath = await this.getOutputFilePath(outputFileName);
|
|
|
|
// Run FFmpeg command
|
|
await this.runFFmpegCommand(inputBuffer, outputPath, quality);
|
|
|
|
console.log('Video compression complete!');
|
|
}
|
|
|
|
private async loadFFmpeg(): Promise<void> {
|
|
if (!FFmpeg.isLoaded()) {
|
|
await FFmpeg.load();
|
|
}
|
|
}
|
|
|
|
private async fetchVideo(url: string): Promise<ArrayBuffer> {
|
|
const response = await this.http.get(url, { responseType: 'arraybuffer' }).toPromise();
|
|
return response;
|
|
}
|
|
|
|
private async getOutputFilePath(outputFileName: string): Promise<string> {
|
|
const dataDirectory = await Filesystem.getUri({
|
|
directory: FilesystemDirectory.Data,
|
|
path: ''
|
|
});
|
|
|
|
return `${dataDirectory.uri}/${outputFileName}`;
|
|
}
|
|
|
|
private async runFFmpegCommand(inputBuffer: ArrayBuffer, outputPath: string, quality: string): Promise<void> {
|
|
await FFmpeg.run('-i', inputBuffer, '-c:v', 'libx264', '-preset', quality, outputPath);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
//Mobile
|
|
/* import { Injectable } from '@angular/core';
|
|
import { HTTP } from '@ionic-native/http/ngx';
|
|
import { Filesystem, FilesystemDirectory, FilesystemEncoding } from '@capacitor/filesystem';
|
|
import { Platform } from '@ionic/angular';
|
|
|
|
declare var FFmpeg: any;
|
|
|
|
@Injectable({
|
|
providedIn: 'root'
|
|
})
|
|
export class VideoCompressionService {
|
|
|
|
constructor(
|
|
private http: HTTP,
|
|
private platform: Platform
|
|
) { }
|
|
|
|
async compressVideo(inputFile: string, outputFileName: string, quality: string = 'medium'): Promise<void> {
|
|
// Load FFmpeg
|
|
await this.loadFFmpeg();
|
|
|
|
// Input file
|
|
const inputBuffer = await this.fetchVideo(inputFile);
|
|
|
|
// Output file path
|
|
const outputPath = await this.getOutputFilePath(outputFileName);
|
|
|
|
// Run FFmpeg command
|
|
await this.runFFmpegCommand(inputBuffer, outputPath, quality);
|
|
|
|
console.log('Video compression complete!');
|
|
}
|
|
|
|
private async loadFFmpeg(): Promise<void> {
|
|
if (!FFmpeg.isLoaded()) {
|
|
await FFmpeg.load();
|
|
}
|
|
}
|
|
|
|
private async fetchVideo(url: string): Promise<ArrayBuffer> {
|
|
const response = await this.http.sendRequest(url, {
|
|
method: 'get',
|
|
responseType: 'arraybuffer'
|
|
});
|
|
|
|
return response.data;
|
|
}
|
|
|
|
private async getOutputFilePath(outputFileName: string): Promise<string> {
|
|
const dataDirectory = await Filesystem.getUri({
|
|
directory: FilesystemDirectory.Data,
|
|
path: ''
|
|
});
|
|
|
|
return `${dataDirectory.uri}/${outputFileName}`;
|
|
}
|
|
|
|
private async runFFmpegCommand(inputBuffer: ArrayBuffer, outputPath: string, quality: string): Promise<void> {
|
|
await FFmpeg.run('-i', inputBuffer, '-c:v', 'libx264', '-preset', quality, outputPath);
|
|
}
|
|
}
|
|
*/ |