jbilcke-hf HF staff commited on
Commit
a88efe5
1 Parent(s): 5cb647a
Files changed (2) hide show
  1. Dockerfile +5 -0
  2. src/app/api/utils/getMediaInfo.ts +15 -13
Dockerfile CHANGED
@@ -45,6 +45,11 @@ COPY . .
45
 
46
  # RUN yarn build
47
 
 
 
 
 
 
48
  # If you use yarn, comment out this line and use the line above
49
  RUN npm run build
50
 
 
45
 
46
  # RUN yarn build
47
 
48
+ # For FFMPEG and gl concat
49
+ RUN apk add curl python3 python3-dev libx11-dev libsm-dev libxrender libxext-dev mesa-dev xvfb libxi-dev glew-dev
50
+
51
+ RUN apk add --no-cache ffmpeg
52
+
53
  # If you use yarn, comment out this line and use the line above
54
  RUN npm run build
55
 
src/app/api/utils/getMediaInfo.ts CHANGED
@@ -1,8 +1,8 @@
1
- import ffmpeg from "fluent-ffmpeg";
 
 
2
 
3
- import { tmpdir } from "node:os";
4
- import { promises as fs } from "node:fs";
5
- import { join } from "node:path";
6
 
7
  export type MediaMetadata = {
8
  durationInSec: number;
@@ -19,10 +19,11 @@ export async function getMediaInfo(input: string): Promise<MediaMetadata> {
19
  // If the input is a base64 string
20
  if (input.startsWith("data:")) {
21
  // Extract the base64 content
22
- const [head, tail] = input.split(";base64,")
23
- if (!tail) {
24
- throw new Error("Invalid base64 data");
25
- }
 
26
 
27
  const extension = head.split("/").pop() || ""
28
  const base64Content = tail || ""
@@ -31,16 +32,17 @@ export async function getMediaInfo(input: string): Promise<MediaMetadata> {
31
  const buffer = Buffer.from(base64Content, 'base64')
32
 
33
  // Generate a temporary file name
34
- const tempFileName = join(tmpdir(), `temp-media-${Date.now()}`);
 
35
 
36
  // Write the buffer to a temporary file
37
- await fs.writeFile(tempFileName, buffer);
38
 
39
  // Get metadata from the temporary file then delete the file
40
  try {
41
  return await getMetaDataFromPath(tempFileName);
42
  } finally {
43
- await fs.rm(tempFileName);
44
  }
45
  }
46
 
@@ -59,7 +61,7 @@ async function getMetaDataFromPath(filePath: string): Promise<MediaMetadata> {
59
  }
60
 
61
  if (err) {
62
- console.error("getMediaInfo(): failed to analyze the source (might happen with empty files)", err)
63
  // reject(err);
64
  resolve(results);
65
  return;
@@ -71,7 +73,7 @@ async function getMetaDataFromPath(filePath: string): Promise<MediaMetadata> {
71
  results.hasAudio = (metadata?.streams || []).some((stream) => stream.codec_type === 'audio');
72
 
73
  } catch (err) {
74
- console.error(`getMediaInfo(): failed to analyze the source (might happen with empty files)`, err)
75
  results.durationInSec = 0
76
  results.durationInMs = 0
77
  results.hasAudio = false
 
1
+ import { tmpdir } from "node:os"
2
+ import { writeFile, rm } from "node:fs/promises"
3
+ import { join } from "node:path"
4
 
5
+ import ffmpeg from "fluent-ffmpeg"
 
 
6
 
7
  export type MediaMetadata = {
8
  durationInSec: number;
 
19
  // If the input is a base64 string
20
  if (input.startsWith("data:")) {
21
  // Extract the base64 content
22
+ // Extract the base64 content
23
+ const [head, tail] = input.split(";base64,")
24
+ if (!tail) {
25
+ throw new Error("Invalid base64 data");
26
+ }
27
 
28
  const extension = head.split("/").pop() || ""
29
  const base64Content = tail || ""
 
32
  const buffer = Buffer.from(base64Content, 'base64')
33
 
34
  // Generate a temporary file name
35
+ const tempFileName = join(tmpdir(), `temp-media-${Date.now()}.${extension}`);
36
+
37
 
38
  // Write the buffer to a temporary file
39
+ await writeFile(tempFileName, buffer);
40
 
41
  // Get metadata from the temporary file then delete the file
42
  try {
43
  return await getMetaDataFromPath(tempFileName);
44
  } finally {
45
+ await rm(tempFileName);
46
  }
47
  }
48
 
 
61
  }
62
 
63
  if (err) {
64
+ console.error("getMediaInfo(): failed to analyze the source (might happen with empty files)")
65
  // reject(err);
66
  resolve(results);
67
  return;
 
73
  results.hasAudio = (metadata?.streams || []).some((stream) => stream.codec_type === 'audio');
74
 
75
  } catch (err) {
76
+ console.error(`getMediaInfo(): failed to analyze the source (might happen with empty files)`)
77
  results.durationInSec = 0
78
  results.durationInMs = 0
79
  results.hasAudio = false