Skip to content

Instantly share code, notes, and snippets.

@pkpio
Created February 17, 2025 20:24
Show Gist options
  • Select an option

  • Save pkpio/25e036810262b6f90201cb10c48fdc4f to your computer and use it in GitHub Desktop.

Select an option

Save pkpio/25e036810262b6f90201cb10c48fdc4f to your computer and use it in GitHub Desktop.
import axios from 'axios';
import fs from 'fs';
import path from 'path';
interface UploadPart {
ETag: string;
PartNumber: string;
}
async function uploadAudioFile(
filePath: string,
spaceId: string,
recordingId: string,
apiKey: string,
chunkSize: number = 10_485_760 // 10MB chunks
) {
const apiClient = axios.create({
baseURL: 'https://transcript.lol/api/v1',
headers: {
'x-api-key': apiKey
}
});
// 1. Start upload
const startUploadResponse = await apiClient.post(
`/spaces/${spaceId}/recordings/${recordingId}/uploads`,
{ mimeType: 'audio/mp3' }
);
const { uploadId } = startUploadResponse.data;
// 2. Read and chunk file
const fileBuffer = fs.readFileSync(filePath);
const chunks: Buffer[] = [];
for (let i = 0; i < fileBuffer.length; i += chunkSize) {
chunks.push(fileBuffer.slice(i, i + chunkSize));
}
// 3. Upload chunks
const completedParts: UploadPart[] = [];
for (let i = 0; i < chunks.length; i++) {
const partNumber = (i + 1).toString();
// Get upload URL for this part
const permitResponse = await apiClient.post(
`/spaces/${spaceId}/recordings/${recordingId}/uploads/${uploadId}/parts/${partNumber}`
);
const { partUploadUrl } = permitResponse.data;
// Upload the chunk directly to S3
const uploadResponse = await axios.put(partUploadUrl, chunks[i], {
headers: { 'Content-Type': 'application/octet-stream' }
});
completedParts.push({
ETag: uploadResponse.headers.etag.replace(/"/g, ''),
PartNumber: partNumber
});
}
// 4. Complete upload
await apiClient.post(
`/spaces/${spaceId}/recordings/${recordingId}/uploads/${uploadId}/complete`,
{ parts: completedParts }
);
}
// Example usage:
const apiKey = 'your-api-key';
const spaceId = 'your-workspace-id';
const recordingId = 'your-recording-id';
const filePath = '/path/to/audio.mp3';
await uploadAudioFile(filePath, spaceId, recordingId, apiKey);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment