-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
124 additions
and
11 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
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,19 @@ | ||
interface VideoInfo { | ||
size: number; | ||
width: number; | ||
height: number; | ||
duration: number; | ||
} | ||
|
||
interface ConvertVideoOptions { | ||
type?: 'AV1'; | ||
poster?: string; | ||
maxWidth: number; | ||
maxHeight: number; | ||
} | ||
|
||
export declare function convertVideo( | ||
source: string, | ||
target: string, | ||
options: ConvertVideoOptions | ||
): Promise<VideoInfo>; |
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,78 @@ | ||
import { IO, env } from '#utils/io.js'; | ||
import { UnProcessable } from '../exceptions/UnProcessable.js'; | ||
|
||
const PATH_FFMPEG = env.PATH_FFMPEG || 'ffmpeg'; | ||
const PATH_FFPROBE = env.PATH_FFPROBE || 'ffprobe'; | ||
|
||
export async function getVideoInfo(path) { | ||
const { streams } = JSON.parse( | ||
await IO.exec( | ||
`${PATH_FFPROBE} -v error -print_format json -show_streams -select_streams v "${path}"` | ||
) | ||
); | ||
|
||
if (streams?.length) { | ||
return streams[0]; | ||
} else { | ||
throw new UnProcessable('Not found video streams'); | ||
} | ||
} | ||
|
||
export async function convertVideo(source, target, options) { | ||
let { width, height, duration: ds } = await getVideoInfo(source); | ||
let duration = Math.round(Number(ds)); | ||
|
||
let cmd = PATH_FFMPEG + ' -i "' + source + '" -v error'; | ||
|
||
if (width > options.maxWidth || height > options.maxHeight) { | ||
if (width > height) { | ||
height = Math.round(options.maxHeight / (width / height)); | ||
width = options.maxWidth; | ||
|
||
cmd += ' -vf "scale=' + width + ':-1"'; | ||
} else { | ||
width = Math.round(options.maxWidth / (height / width)); | ||
height = options.maxHeight; | ||
|
||
cmd += ' -vf "scale=-1:' + height + '"'; | ||
} | ||
} | ||
|
||
if (options.type === 'AV1') { | ||
cmd += ' -c:v libsvtav1'; | ||
cmd += ' -preset 10'; | ||
cmd += ' -crf 36'; | ||
cmd += ' -pix_fmt yuv420p10le'; | ||
cmd += ' -svtav1-params fast-decode=1'; | ||
} else { | ||
cmd += ' -c:v copy'; | ||
} | ||
|
||
cmd += ' -c:a copy'; | ||
cmd += ' -y "' + target + '"'; | ||
|
||
await IO.exec(cmd); | ||
|
||
if (options.poster) { | ||
const time = duration / 10; | ||
|
||
await IO.exec( | ||
`${PATH_FFMPEG} -y -v error -ss ${time} -i "${target}" -frames:v 1 "${options.poster}"` | ||
); | ||
} | ||
|
||
return { width, height, duration, size: IO.getFileSize(target) }; | ||
} | ||
|
||
// console.log( | ||
// await convertVideo( | ||
// '/Users/oleksiiskydan/Desktop/Labs/Files/2.mp4', | ||
// '/Users/oleksiiskydan/Desktop/Labs/Files/aa.mkv', | ||
// { | ||
// type: 'AV1', | ||
// maxWidth: 1280, | ||
// maxHeight: 1280, | ||
// poster: '/Users/oleksiiskydan/Desktop/Labs/Files/aa.avif', | ||
// } | ||
// ) | ||
// ); |
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