|
1 | 1 | import ffmpeg from "fluent-ffmpeg"; |
2 | | -// import createError from "http-errors"; |
3 | | - |
4 | | -// import { MESSAGES } from "../config/constants.js"; |
| 2 | +import createError from "http-errors"; |
| 3 | +import { MESSAGES } from "../config/constants.js"; |
5 | 4 | import env from "../config/env.js"; |
6 | 5 | import { bucket } from "../config/gcs.js"; |
7 | 6 |
|
8 | 7 | ffmpeg.setFfmpegPath("/usr/bin/ffmpeg"); |
9 | 8 |
|
10 | | -const trimVideo = async ({ videoId, trimStart, trimEnd }) => { |
| 9 | +const trimAndUpload = async ({ videoId, trimStart, trimEnd, outputPath }) => { |
11 | 10 | const originalVideo = `${env.ORIGINAL_PREFIX}/${videoId}`; |
12 | 11 |
|
13 | | - try { |
14 | | - const stream = bucket.file(originalVideo).createReadStream(); |
15 | | - return new Promise((resolve, reject) => { |
16 | | - const ffmpegStream = ffmpeg(stream) |
17 | | - .setStartTime(trimStart) |
18 | | - .duration(trimEnd - trimStart) |
19 | | - .format("webm") |
20 | | - .on("error", (err) => reject(err)); |
21 | | - |
22 | | - const outputStream = ffmpegStream.pipe(); |
23 | | - resolve(outputStream); |
| 12 | + return new Promise((resolve, reject) => { |
| 13 | + const inputFile = bucket.file(originalVideo); |
| 14 | + const inputStream = inputFile.createReadStream(); |
| 15 | + |
| 16 | + inputStream.on("error", (err) => { |
| 17 | + console.error("Failed to read input video:", err); |
| 18 | + reject(createError.InternalServerError("Failed to read video")); |
| 19 | + }); |
| 20 | + |
| 21 | + inputStream.on("response", (res) => { |
| 22 | + console.log( |
| 23 | + "Started reading original video. Status code:", |
| 24 | + res.statusCode |
| 25 | + ); |
24 | 26 | }); |
25 | | - } catch (err) { |
26 | | - throw err; |
27 | | - } |
| 27 | + |
| 28 | + const outputFile = bucket.file(outputPath); |
| 29 | + const outputStream = outputFile.createWriteStream({ |
| 30 | + metadata: { contentType: "video/webm" }, |
| 31 | + }); |
| 32 | + |
| 33 | + outputStream.on("error", (err) => { |
| 34 | + console.error("Failed to save video to GCS:", err); |
| 35 | + reject(createError.InternalServerError(MESSAGES.ERROR.FAILED_SAVE_VIDEO)); |
| 36 | + }); |
| 37 | + |
| 38 | + outputStream.on("finish", async () => { |
| 39 | + console.log("Upload to GCS finished."); |
| 40 | + |
| 41 | + try { |
| 42 | + const [meta] = await outputFile.getMetadata(); |
| 43 | + console.log("Saved video size:", meta.size, "bytes"); |
| 44 | + } catch (err) { |
| 45 | + console.error("Failed to get uploaded file metadata:", err); |
| 46 | + } |
| 47 | + |
| 48 | + resolve(); |
| 49 | + }); |
| 50 | + |
| 51 | + console.log("Starting video trimming with FFmpeg..."); |
| 52 | + ffmpeg(inputStream) |
| 53 | + .setStartTime(trimStart) |
| 54 | + .duration(trimEnd - trimStart) |
| 55 | + .format("webm") |
| 56 | + .on("start", (commandLine) => { |
| 57 | + console.log("FFmpeg command:", commandLine); |
| 58 | + }) |
| 59 | + .on("error", (err) => { |
| 60 | + console.error("FFmpeg processing error:", err); |
| 61 | + reject( |
| 62 | + createError.InternalServerError(MESSAGES.ERROR.FAILED_EDIT_VIDEO) |
| 63 | + ); |
| 64 | + }) |
| 65 | + .on("end", () => { |
| 66 | + console.log("FFmpeg trimming finished."); |
| 67 | + }) |
| 68 | + .pipe(outputStream, { end: true }); |
| 69 | + }); |
28 | 70 | }; |
29 | 71 |
|
30 | | -export default trimVideo; |
| 72 | +export default trimAndUpload; |
0 commit comments