Skip to content

Commit

Permalink
transcoding and server side uploads POC
Browse files Browse the repository at this point in the history
  • Loading branch information
zeeshanakram3 committed Aug 3, 2024
1 parent 5eeeb68 commit 1072c18
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/upload-server/PROGRESS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

# Tracking/Specifications issue

https://github.com/Joystream/joystream/issues/4761
43 changes: 43 additions & 0 deletions src/upload-server/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import * as tus from 'tus-js-client'

Check failure on line 1 in src/upload-server/client.ts

View workflow job for this annotation

GitHub Actions / Run migrations

Cannot find module 'tus-js-client' or its corresponding type declarations.

Check failure on line 1 in src/upload-server/client.ts

View workflow job for this annotation

GitHub Actions / Run migrations

Cannot find name 'input'. Did you mean 'oninput'?

Check failure on line 1 in src/upload-server/client.ts

View workflow job for this annotation

GitHub Actions / Run migrations

Parameter 'e' implicitly has an 'any' type.

Check failure on line 1 in src/upload-server/client.ts

View workflow job for this annotation

GitHub Actions / Run migrations

Parameter 'error' implicitly has an 'any' type.

Check failure on line 1 in src/upload-server/client.ts

View workflow job for this annotation

GitHub Actions / Run migrations

Parameter 'bytesUploaded' implicitly has an 'any' type.

Check failure on line 1 in src/upload-server/client.ts

View workflow job for this annotation

GitHub Actions / Run migrations

Parameter 'bytesTotal' implicitly has an 'any' type.

Check failure on line 1 in src/upload-server/client.ts

View workflow job for this annotation

GitHub Actions / Run migrations

Parameter 'previousUploads' implicitly has an 'any' type.

Check failure on line 1 in src/upload-server/client.ts

View workflow job for this annotation

GitHub Actions / Run migrations

Cannot find module 'tus-js-client' or its corresponding type declarations.

Check failure on line 1 in src/upload-server/client.ts

View workflow job for this annotation

GitHub Actions / Run migrations

Cannot find name 'input'. Did you mean 'oninput'?

Check failure on line 1 in src/upload-server/client.ts

View workflow job for this annotation

GitHub Actions / Run migrations

Parameter 'e' implicitly has an 'any' type.

Check failure on line 1 in src/upload-server/client.ts

View workflow job for this annotation

GitHub Actions / Run migrations

Parameter 'error' implicitly has an 'any' type.

Check failure on line 1 in src/upload-server/client.ts

View workflow job for this annotation

GitHub Actions / Run migrations

Parameter 'bytesUploaded' implicitly has an 'any' type.

Check failure on line 1 in src/upload-server/client.ts

View workflow job for this annotation

GitHub Actions / Run migrations

Parameter 'bytesTotal' implicitly has an 'any' type.

Check failure on line 1 in src/upload-server/client.ts

View workflow job for this annotation

GitHub Actions / Run migrations

Parameter 'previousUploads' implicitly has an 'any' type.

input.addEventListener('change', function (e) {
// Get the selected file from the input element
var file = e.target.files[0]

Check failure on line 5 in src/upload-server/client.ts

View workflow job for this annotation

GitHub Actions / Local build, linting and formatting (ubuntu-latest, 18.x)

Unexpected var, use let or const instead

// Create a new tus upload
var upload = new tus.Upload(file, {

Check failure on line 8 in src/upload-server/client.ts

View workflow job for this annotation

GitHub Actions / Local build, linting and formatting (ubuntu-latest, 18.x)

Unexpected var, use let or const instead
// Endpoint is the upload creation URL from your tus server
endpoint: 'http://localhost:1080/files/',
// Retry delays will enable tus-js-client to automatically retry on errors
retryDelays: [0, 3000, 5000, 10000, 20000],
// Attach additional meta data about the file for the server
metadata: {
filename: file.name,
filetype: file.type,
},
// Callback for errors which cannot be fixed using retries
onError: function (error) {
console.log('Failed because: ' + error)
},
// Callback for reporting upload progress
onProgress: function (bytesUploaded, bytesTotal) {
var percentage = ((bytesUploaded / bytesTotal) * 100).toFixed(2)

Check failure on line 24 in src/upload-server/client.ts

View workflow job for this annotation

GitHub Actions / Local build, linting and formatting (ubuntu-latest, 18.x)

Unexpected var, use let or const instead
console.log(bytesUploaded, bytesTotal, percentage + '%')
},
// Callback for once the upload is completed
onSuccess: function () {
console.log('Download %s from %s', upload.file.name, upload.url)
},
})

// Check if there are any previous uploads to continue.
upload.findPreviousUploads().then(function (previousUploads) {
// Found previous uploads so we select the first one.
if (previousUploads.length) {
upload.resumeFromPreviousUpload(previousUploads[0])
}

// Start the upload
upload.start()
})
})
30 changes: 30 additions & 0 deletions src/upload-server/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { S3Store } from '@tus/s3-store'

Check failure on line 1 in src/upload-server/server.ts

View workflow job for this annotation

GitHub Actions / Run migrations

Cannot find module '@tus/s3-store' or its corresponding type declarations.

Check failure on line 1 in src/upload-server/server.ts

View workflow job for this annotation

GitHub Actions / Run migrations

Cannot find module '@tus/server' or its corresponding type declarations.

Check failure on line 1 in src/upload-server/server.ts

View workflow job for this annotation

GitHub Actions / Run migrations

Cannot find module '@tus/s3-store' or its corresponding type declarations.

Check failure on line 1 in src/upload-server/server.ts

View workflow job for this annotation

GitHub Actions / Run migrations

Cannot find module '@tus/server' or its corresponding type declarations.
import { Server } from '@tus/server'
import express from 'express'

const s3Store = new S3Store({
partSize: 8 * 1024 * 1024, // Each uploaded part will have ~8MiB,
s3ClientConfig: {
bucket: process.env.AWS_BUCKET || '',
region: process.env.AWS_REGION || '',
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID || '',
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY || '',
},
},
})

const host = '127.0.0.1'
const port = 1080
const app = express()
const uploadApp = express()

const server = new Server({
path: '/uploads',
datastore: s3Store,
})

uploadApp.all('*', server.handle.bind(server))
app.use('/uploads', uploadApp)
app.listen(port, host)
console.log(`Server is running at http://${host}:${port}`)
53 changes: 53 additions & 0 deletions src/upload-server/transcoding.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import ffmpeg, { FfmpegCommand } from 'fluent-ffmpeg'
import path from 'path'

// Define input and output file paths
const inputFilePath: string = path.join(__dirname, 'input.mp4')
const outputFilePath: string = path.join(__dirname, 'output.avi')

// Interface for FFmpeg progress information
interface FfmpegProgress {
percent: number
}

// Function to transcode the video
function transcodeVideo(input: string, output: string): Promise<void> {
return new Promise((resolve, reject) => {
const command: FfmpegCommand = ffmpeg(input)
.output(output)
.on('start', (commandLine: string) => {
console.log('Spawned FFmpeg with command: ' + commandLine)
})
.on('progress', (progress: FfmpegProgress) => {
console.log(`Processing: ${progress.percent.toFixed(2)}% done`)
})
.on('error', (err: Error, stdout: string, stderr: string) => {
console.log('Error occurred: ' + err.message)
console.log('FFmpeg stderr: ' + stderr)
reject(err)
})
.on('end', () => {
console.log('Transcoding finished successfully')
resolve()
})

// Run the command

command
.videoCodec('libx264')
.audioCodec('aac')
.videoBitrate('1000k')
.audioBitrate('128k')
.size('640x480')
.run()
})
}

// Execute transcoding
transcodeVideo(inputFilePath, outputFilePath)
.then(() => {
console.log('Transcoding completed successfully')
})
.catch((error: Error) => {
console.error('Error during transcoding:', error)
})

0 comments on commit 1072c18

Please sign in to comment.