-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode.ts
79 lines (68 loc) · 2.01 KB
/
encode.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { fileExists } from "./utils.ts"
export async function encode(key: number, segment: number[], crf: number, segmentPath: string, outPath: string, retries = 0): Promise<void> {
if (await fileExists(`${outPath}/${key}/${crf}.webm`)) {
console.log(`Skipping encoding ${key} with crf ${crf} because file already exists`)
return
}
const pSvt = Deno.run({
cmd: [
"SvtAv1EncApp",
"-i",
segmentPath,
"-b",
`/dev/stdout`,
"-w",
"1920",
"-h",
"1080",
"--fps",
"23.98",
"--color-primaries",
"bt709",
"--transfer-characteristics",
"bt709",
"--matrix-coefficients",
"bt709",
"--lp",
retries ? "32" : "1",
"--rc",
"0",
"--qp",
`${crf}`,
"--preset",
"12",
],
stdout: "piped",
stdin: "piped",
stderr: "piped",
})
// ffmpeg -y -i - -c copy -an out.webm
const pFfmpeg = Deno.run({
cmd: [
"ffmpeg",
"-y",
"-i",
"-",
"-c",
"copy",
"-an",
`${outPath}/${key}/${crf}.webm`
],
stdin: "piped",
stdout: "piped",
stderr: "piped",
})
pSvt.stdout.readable.pipeTo(pFfmpeg.stdin.writable)
const { code } = await pFfmpeg.status()
if (code !== 0) {
console.log(`Encoding segment ${key} with crf ${crf} failed`)
// console.log(new TextDecoder().decode(await pSvt.stderrOutput()))
// console.log(new TextDecoder().decode(await pFfmpeg.stderrOutput()))
if (retries > 1) {
return
}
Deno.remove(`${outPath}/${key}/${crf}.webm`)
return await encode(key, segment, crf, segmentPath, outPath, retries + 1)
}
console.log(`Encoding segment ${key} with crf ${crf} successful`)
}