Skip to content

Commit

Permalink
feat: add video converter
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlaPaiva committed Jan 27, 2024
1 parent bb47ce5 commit da27dcd
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/components/converter-content/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import VideoConverter from '../video-converter';

const items = [
{
label: 'Video To GIF',
label: 'Video Converter',
key: '1',
children: <VideoConverter/>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const UploadStep = memo(function UploadStepComponent(props: Readonly<UploadStepP
props.allowedUploadingFormats.forEach((type, index) => {
if (index === props.allowedUploadingFormats.length - 1) {
types += type
return
}

types += type + ", "
Expand Down
19 changes: 8 additions & 11 deletions src/components/video-converter/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,31 @@ import { Format } from "../../model/format"
import Converter from "../converter"

const uploadingFormat: string[] = [
".mp4"
"video/mp4",
"video/x-m4v",
"video/*"
]

const optionsToConvertTo: Format[] = [
{
name: "GIF",
extension: ".gif",
mimeType: "image/gif"
extension: ".gif"
},
{
name: "MP4",
extension: ".mp4",
mimeType: "video/mp4"
extension: ".mp4"
},
{
name: "AVI",
extension: ".avi",
mimeType: "video/x-msvideo"
extension: ".avi"
},
{
name: "WMV",
extension: ".wmv",
mimeType: "video/x-ms-asf"
extension: ".wmv"
},
{
name: "MP3",
extension: ".mp3",
mimeType: "audio/mp4"
extension: ".mp3"
}
]

Expand Down
2 changes: 0 additions & 2 deletions src/model/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@ export type Format = {
name: string
/** Extension with a dot */
extension: string
/** Mime type of this format, that allow it to be downloaded */
mimeType: string
}
29 changes: 23 additions & 6 deletions src/services/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ import { createFFmpeg } from "@ffmpeg/ffmpeg";
import { UploadFile } from "antd";
import { ConvertedFile } from "../model/converted-file";

const mimeType = {
".gif": "image/gif",
".mp4": "video/mp4",
".avi": "video/x-msvideo",
".wmv": "video/x-ms-wmv",
".mp3": "audio/mp3",
}

async function convertFiles(fileList: UploadFile[], extension: string): Promise<ConvertedFile[]>{
const blobList: ConvertedFile[] = []

Expand All @@ -23,12 +31,21 @@ async function convertFiles(fileList: UploadFile[], extension: string): Promise<
await ffmpeg.run("-i", file.name, outputName);

const output = ffmpeg.FS("readFile", outputName);
const blob = new Blob([output.buffer], { type: "image/gif" })
blobList.push({
newFileName: outputName,
blob,
result: "success"
})

if (output === null || output.buffer.byteLength <= 0 ) {
blobList.push({
newFileName: outputName,
result: "error",
blob: new Blob()
})
} else {
const blob = new Blob([output.buffer], { type: mimeType[extension as keyof typeof mimeType] })
blobList.push({
newFileName: outputName,
blob,
result: "success"
})
}
}

return blobList
Expand Down

0 comments on commit da27dcd

Please sign in to comment.