Skip to content

Commit

Permalink
comment out the downloadNecessaryHLS code (#198)
Browse files Browse the repository at this point in the history
  • Loading branch information
Leslie-Wong-H authored Aug 2, 2023
2 parents f269020 + f81226a commit d085922
Showing 1 changed file with 90 additions and 86 deletions.
176 changes: 90 additions & 86 deletions src/lib/generate-video-preview.js
Original file line number Diff line number Diff line change
@@ -1,97 +1,100 @@
import fs from "fs-extra";
import path from "path";
import os from "os";
import crypto from "crypto";
import fetch from "node-fetch";
// import fs from "fs-extra";
// import path from "path";
// import os from "os";
// import crypto from "crypto";
// import fetch from "node-fetch";
import child_process from "child_process";
import { S3Client, GetObjectCommand } from "@aws-sdk/client-s3";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
// import { S3Client, GetObjectCommand } from "@aws-sdk/client-s3";
// import { getSignedUrl } from "@aws-sdk/s3-request-presigner";

const { AWS_ENDPOINT_URL, AWS_ACCESS_KEY, AWS_SECRET_KEY, AWS_BUCKET, AWS_REGION } = process.env;
// const { AWS_ENDPOINT_URL, AWS_ACCESS_KEY, AWS_SECRET_KEY, AWS_BUCKET, AWS_REGION } = process.env;

const opts = AWS_ENDPOINT_URL
? {
forcePathStyle: true,
endpoint: AWS_ENDPOINT_URL,
region: AWS_REGION,
credentials: {
accessKeyId: AWS_ACCESS_KEY,
secretAccessKey: AWS_SECRET_KEY,
},
}
: {};
// const opts = AWS_ENDPOINT_URL
// ? {
// forcePathStyle: true,
// endpoint: AWS_ENDPOINT_URL,
// region: AWS_REGION,
// credentials: {
// accessKeyId: AWS_ACCESS_KEY,
// secretAccessKey: AWS_SECRET_KEY,
// },
// }
// : {};

const s3 = new S3Client(opts);
// const s3 = new S3Client(opts);

let command;
// let command;

export default async (filePath, start, end, key, size = "m", mute = false) => {
let targetPath = "";
const tempPath = path.join(os.tmpdir(), crypto.createHash("md5").update(filePath).digest("hex"));
fs.ensureDirSync(tempPath);
// Remote S3 HLS
if (filePath.includes("index.m3u8")) {
const tempIndexPath = path.join(tempPath, "index.m3u8");
/**
* (not work, see issue #197)
*/
// let targetPath = "";
// const tempPath = path.join(os.tmpdir(), crypto.createHash("md5").update(filePath).digest("hex"));
// fs.ensureDirSync(tempPath);
// // Remote S3 HLS
// if (filePath.includes("index.m3u8")) {
// const tempIndexPath = path.join(tempPath, "index.m3u8");

const response = await fetch(filePath);
const downloadNecessaryHLS = (res, indexPath) => {
return new Promise((resolve) => {
res.body.pipe(fs.createWriteStream(indexPath));
res.body.on("end", async () => {
console.log(`Fetched ${indexPath}`);
const cont = await fs.readFile(indexPath, { encoding: "utf8" });
const lines = cont.split("\n");
//Issue #186, prevent hls Error when loading first segment
let tsList = ["10s_000.ts"];
lines.reduce((acc, curV, curI) => {
let re = /^#EXTINF:(?<num>\d+\.?\d*),/;
let match = re.exec(curV);
let sum = acc;
if (match) {
const {
groups: { num },
} = match;
sum = acc + Number(num);
if (start >= acc && start <= sum) {
// allow over edge
tsList.push(`10s_${(curI - 2 + "").padStart(3, "0")}.ts`);
tsList.push(`10s_${(curI - 1 + "").padStart(3, "0")}.ts`);
tsList.push(`10s_${(curI + "").padStart(3, "0")}.ts`);
tsList.push(`10s_${(curI + 1 + "").padStart(3, "0")}.ts`);
tsList.push(`10s_${(curI + 2 + "").padStart(3, "0")}.ts`);
}
}
return sum;
}, 0);
let signedUrl = "";
let params;
for (const ts of tsList) {
try {
params = {
Bucket: AWS_BUCKET,
Key: `${key}/${ts}`,
};
command = new GetObjectCommand(params);
signedUrl = await getSignedUrl(s3, command, { expiresIn: 60 * 5 });
const tsResponse = await fetch(signedUrl);
const tsArrayBuffer = await tsResponse.arrayBuffer();
fs.writeFileSync(path.join(tempPath, ts), Buffer.from(tsArrayBuffer));
} catch (error) {
console.log(error);
}
}
await resolve("ok");
});
});
};
// const response = await fetch(filePath);
// const downloadNecessaryHLS = (res, indexPath) => {
// return new Promise((resolve) => {
// res.body.pipe(fs.createWriteStream(indexPath));
// res.body.on("end", async () => {
// console.log(`Fetched ${indexPath}`);
// const cont = await fs.readFile(indexPath, { encoding: "utf8" });
// const lines = cont.split("\n");
// //Issue #186, prevent hls Error when loading first segment
// let tsList = ["10s_000.ts"];
// lines.reduce((acc, curV, curI) => {
// let re = /^#EXTINF:(?<num>\d+\.?\d*),/;
// let match = re.exec(curV);
// let sum = acc;
// if (match) {
// const {
// groups: { num },
// } = match;
// sum = acc + Number(num);
// if (start >= acc && start <= sum) {
// // allow over edge
// tsList.push(`10s_${(curI - 2 + "").padStart(3, "0")}.ts`);
// tsList.push(`10s_${(curI - 1 + "").padStart(3, "0")}.ts`);
// tsList.push(`10s_${(curI + "").padStart(3, "0")}.ts`);
// tsList.push(`10s_${(curI + 1 + "").padStart(3, "0")}.ts`);
// tsList.push(`10s_${(curI + 2 + "").padStart(3, "0")}.ts`);
// }
// }
// return sum;
// }, 0);
// let signedUrl = "";
// let params;
// for (const ts of tsList) {
// try {
// params = {
// Bucket: AWS_BUCKET,
// Key: `${key}/${ts}`,
// };
// command = new GetObjectCommand(params);
// signedUrl = await getSignedUrl(s3, command, { expiresIn: 60 * 5 });
// const tsResponse = await fetch(signedUrl);
// const tsArrayBuffer = await tsResponse.arrayBuffer();
// fs.writeFileSync(path.join(tempPath, ts), Buffer.from(tsArrayBuffer));
// } catch (error) {
// console.log(error);
// }
// }
// await resolve("ok");
// });
// });
// };

await downloadNecessaryHLS(response, tempIndexPath);
// await downloadNecessaryHLS(response, tempIndexPath);

targetPath = tempIndexPath;
} else {
// Local Minio MP4
targetPath = filePath;
}
// targetPath = tempIndexPath;
// } else {
// // Local Minio MP4
// targetPath = filePath;
// }

const ffmpeg = child_process.spawnSync(
"ffmpeg",
Expand All @@ -109,7 +112,8 @@ export default async (filePath, start, end, key, size = "m", mute = false) => {
"-ss",
start - 10,
"-i",
targetPath,
// targetPath,
filePath,
"-ss",
"10",
"-t",
Expand Down Expand Up @@ -154,6 +158,6 @@ export default async (filePath, start, end, key, size = "m", mute = false) => {
if (ffmpeg.stderr.length) {
console.log(ffmpeg.stderr.toString());
}
fs.rmSync(tempPath, { recursive: true, force: true });
// fs.rmSync(tempPath, { recursive: true, force: true });
return ffmpeg.stdout;
};

0 comments on commit d085922

Please sign in to comment.