-
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
1 parent
1ff2849
commit e1b41b0
Showing
11 changed files
with
159 additions
and
3,057 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,129 @@ | ||
import axios, { AxiosError, AxiosResponse } from 'axios'; | ||
import Ffmpeg from 'fluent-ffmpeg'; | ||
import { S3 } from './s3'; | ||
import { nanoid } from 'nanoid'; | ||
import { createLogger, transports, format } from 'winston'; | ||
import { unlink } from 'node:fs/promises'; | ||
|
||
// Initialize logger | ||
const logger = createLogger({ | ||
level: 'info', | ||
format: format.combine( | ||
format.timestamp(), | ||
format.printf( | ||
({ timestamp, level, message }) => `${timestamp} ${level}: ${message}` | ||
) | ||
), | ||
transports: [new transports.Console()], | ||
}); | ||
|
||
const vimeoAccessToken: string | undefined = process.env.VIMEO_SECRET; | ||
|
||
if (!vimeoAccessToken) { | ||
throw new Error('VIMEO_SECRET environment variable is not set'); | ||
} | ||
|
||
export async function handle(vimeoID: string): Promise<{ videoUrl: string }> { | ||
const videoAPIUrl: string = `https://api.vimeo.com/videos/${vimeoID}`; | ||
|
||
let response: AxiosResponse | null = null; | ||
try { | ||
response = await axios({ | ||
url: videoAPIUrl, | ||
method: 'GET', | ||
headers: { | ||
Authorization: `Bearer ${vimeoAccessToken}`, | ||
}, | ||
}); | ||
} catch (e: any) { | ||
if (e.response?.status === 404) { | ||
throw new Error('Video not found on Vimeo'); | ||
} | ||
|
||
if (e.response.status !== 200) { | ||
throw new Error('Error fetching video from Vimeo'); | ||
} | ||
} | ||
|
||
const files: { height: number; link: string }[] = response?.data?.files; | ||
|
||
if (!files) { | ||
throw new Error('No files found for this video'); | ||
} | ||
|
||
const file540p = files.find((file) => file.height === 540); | ||
|
||
if (!file540p) { | ||
throw new Error('No 540p file found for this video'); | ||
} | ||
const downloadUrl: string = file540p.link; | ||
const downloadResponse = await fetch(downloadUrl); | ||
|
||
await Bun.write('tmp/video.mp4', downloadResponse); | ||
|
||
logger.info('Video downloaded! Processing video...'); | ||
|
||
const videoHash = nanoid(); | ||
const videoUrl = await processVideo( | ||
'tmp/video.mp4', | ||
'tmp/processed-video.mp4', | ||
async () => | ||
saveToAWS( | ||
'tmp/processed-video.mp4', | ||
`workshops/cover-videos/${videoHash}.mp4` | ||
) | ||
); | ||
|
||
// remove temp files | ||
await unlink('tmp/video.mp4'); | ||
await unlink('tmp/processed-video.mp4'); | ||
|
||
return videoUrl; | ||
} | ||
|
||
async function saveToAWS( | ||
videoPath: string, | ||
uploadPath: string | ||
): Promise<string> { | ||
try { | ||
const videoArrBuffer = await Bun.file(videoPath).arrayBuffer(); | ||
const videoBuffer = Buffer.from(videoArrBuffer); | ||
logger.info(`Saving to AWS: ${videoPath}`); | ||
const s3 = new S3(); | ||
const { videoUrl } = await s3.uploadVideo(uploadPath, videoBuffer); | ||
logger.info('Video saved to AWS!'); | ||
logger.info(`Video URL: ${videoUrl}`); | ||
return videoUrl; | ||
} catch (error: any) { | ||
logger.error(`Error saving video to AWS: ${error.message}`); | ||
throw error; | ||
} | ||
} | ||
|
||
export async function processVideo( | ||
inputPath: string, | ||
outputPath: string, | ||
callback: () => Promise<string> | ||
): Promise<{ videoUrl: string }> { | ||
logger.info('Processing video...'); | ||
logger.info(`Input Path: ${inputPath}`); | ||
logger.info(`Output Path: ${outputPath}`); | ||
|
||
return new Promise((resolve, reject) => { | ||
Ffmpeg(inputPath) | ||
.setStartTime('00:00:00') | ||
.setDuration(5) | ||
.noAudio() | ||
.output(outputPath) | ||
.on('end', () => { | ||
callback() | ||
.then((videoUrl) => resolve({ videoUrl })) | ||
.catch((error) => reject(error)); | ||
}) | ||
.on('error', (err: any) => { | ||
logger.error(`Error processing video: ${err.message}`); | ||
reject(err); | ||
}) | ||
.run(); | ||
}); | ||
} |
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,24 @@ | ||
import { Hono } from 'hono'; | ||
import { vimeoVideoValidator } from '../lib/hono-validators'; | ||
import { handle } from '../lib/vimeo-video-thumbnail'; | ||
|
||
const app = new Hono(); | ||
|
||
app.post('/', vimeoVideoValidator(), async (c) => { | ||
const { vimeoID } = c.req.valid('json'); | ||
|
||
try { | ||
// Baixar o vídeo do Vimeo | ||
const { videoUrl } = await handle(vimeoID); | ||
|
||
return c.json({ message: 'Video downloaded from Vimeo', videoUrl }, 200); | ||
} catch (err) { | ||
const errorMessage = err instanceof Error ? err.message : 'Unknown error'; | ||
return c.json( | ||
{ message: `Error downloading the video. ${errorMessage}` }, | ||
500 | ||
); | ||
} | ||
}); | ||
|
||
export default app; |
This file was deleted.
Oops, something went wrong.