-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow for MP4 conversion (#3756)
* feat: allow for MP4 conversion * fix: dont reconvert if converted file already exists * refactor: optimize imports * chore: remove console log * chore: add packages to electron deps * chore: improve migrations * fix: electronApi issues * fix: improvements to font url code --------- Co-authored-by: Manoah Tervoort <[email protected]> Co-authored-by: Manoah Tervoort <[email protected]>
- Loading branch information
1 parent
2e3f093
commit 0b39f08
Showing
23 changed files
with
497 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,6 +88,7 @@ | |
"Kiswahili", | ||
"Kreyòl", | ||
"langwritten", | ||
"libx", | ||
"linux", | ||
"lmdv", | ||
"lorenpajarits", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import { exists } from 'fs-extra'; | ||
import { FULL_HD } from 'src/constants/media'; | ||
import path from 'upath'; | ||
|
||
const { changeExt } = path; | ||
|
||
export const createVideoFromNonVideo = async ( | ||
originalFile: string, | ||
ffmpegPath: string, | ||
) => { | ||
const convertedFilePath = changeExt(originalFile, '.mp4'); | ||
|
||
if (await exists(convertedFilePath)) { | ||
return convertedFilePath; | ||
} | ||
|
||
const { default: ffmpeg } = await import('fluent-ffmpeg'); | ||
|
||
ffmpeg.setFfmpegPath(ffmpegPath); | ||
|
||
if (originalFile.toLowerCase().endsWith('.mp3')) { | ||
await new Promise<void>((resolve, reject) => { | ||
ffmpeg(originalFile) | ||
.noVideo() | ||
.save(convertedFilePath) | ||
.on('end', () => { | ||
resolve(); | ||
}) | ||
.on('error', (err) => { | ||
reject(err); | ||
}); | ||
}); | ||
} else { | ||
const { default: sizeOf } = await import('image-size'); | ||
const { height, orientation, width } = sizeOf(originalFile); | ||
|
||
let adjustedWidth = width; | ||
let adjustedHeight = height; | ||
|
||
if (orientation && orientation >= 5) { | ||
[adjustedWidth, adjustedHeight] = [height, width]; | ||
} | ||
|
||
if (adjustedWidth && adjustedHeight) { | ||
let max = [undefined, Math.min(FULL_HD.height, adjustedHeight)]; | ||
if (FULL_HD.height / FULL_HD.width > adjustedHeight / adjustedWidth) { | ||
max = [Math.min(FULL_HD.width, adjustedWidth), undefined]; | ||
} | ||
const convertedDimensions = resize( | ||
adjustedWidth, | ||
adjustedHeight, | ||
max[0], | ||
max[1], | ||
); | ||
|
||
if (!convertedDimensions) { | ||
throw new Error('Could not determine dimensions of image.'); | ||
} | ||
|
||
await new Promise<void>((resolve, reject) => { | ||
ffmpeg(originalFile) | ||
.inputOptions('-loop 1') | ||
.inputFormat('image2') | ||
.videoCodec('libx264') | ||
.size(`${convertedDimensions.width}x${convertedDimensions.height}`) | ||
// .outputOptions('format=yuv420p', '-r 30') | ||
.outputOptions('-t 5') | ||
.save(convertedFilePath) | ||
.on('end', () => { | ||
resolve(); | ||
}) | ||
.on('error', (err) => { | ||
reject(err); | ||
}); | ||
}); | ||
} else { | ||
throw new Error('Could not determine dimensions of image.'); | ||
} | ||
} | ||
|
||
return convertedFilePath; | ||
}; | ||
|
||
const resize = ( | ||
x: number, | ||
y: number, | ||
xMax?: number, | ||
yMax?: number, | ||
): { height: number; width: number } => { | ||
// Set default values for xMax and yMax if they are undefined | ||
const maxX = xMax ?? Infinity; // Use Infinity if no max value is provided | ||
const maxY = yMax ?? Infinity; | ||
|
||
if (maxX === Infinity && maxY === Infinity) { | ||
throw new Error('No maximum values given.'); | ||
} | ||
|
||
if (maxX !== Infinity && (maxY === Infinity || x / y > maxX / maxY)) { | ||
// Width constrained or aspect ratio favors width. | ||
return { | ||
height: Math.round((maxX * y) / x), | ||
width: maxX, | ||
}; | ||
} | ||
|
||
// Height constrained or aspect ratio favors height. | ||
return { | ||
height: maxY, | ||
width: Math.round((maxY * x) / y), | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.