I’m working on Linkedin api integration in next.js app. Short videos I upload successfully, the problem is with video splitting. My finction split video if necessary and upload it to temp folder. I use ffmpeg from ‘fluent-ffmpeg’;
export async function splitVideoToTemp(fileName: string): Promise<boolean> {
const inputPath = path.join(process.cwd(), 'tmp', 'input', fileName);
ensureFolderExists('chunks');
const outputDir = path.join(process.cwd() + '/tmp/chunks');
const bytes = 4194303;
await splitVideo(inputPath as string, bytes, outputDir);
await deleteFileFromTemp(inputPath);
return true;
}
export async function splitVideo(inputPath: string, chunkSize: number, outputDir:
string): Promise<void> {
try {
await fs.mkdir(outputDir, { recursive: true });
const metadata: FFmpegMetadata = await new Promise((resolve, reject) => {
ffmpeg.ffprobe(inputPath, (err, metadata) => {
if (err) reject(err);
else resolve(metadata as FFmpegMetadata);
});
});
const duration = metadata.format.duration;
const chunkDuration = chunkSize / (metadata.format.size / duration);
const promises = [];
for (let startTime = 0, chunkIndex = 0; startTime < duration; startTime +=
chunkDuration, chunkIndex++) {
const outputPath = path.join(outputDir, `chunk_${chunkIndex}.mp4`);
promises.push(processChunk(inputPath, startTime, chunkDuration, outputPath));
}
await Promise.all(promises);
console.log('Video split successfully');
} catch (error) {
console.error('Error splitting video:', error);
}
}
function processChunk(inputPath: string, startTime: number, chunkDuration: number,
outputPath: string): Promise<void> {
return new Promise((resolve, reject) => {
ffmpeg(inputPath)
.setStartTime(startTime)
.setDuration(chunkDuration)
.output(outputPath)
.on('end', () => resolve())
.on('error', reject)
.run();
});
}
calling this functions nicely split my video to chuks, they are not corrupted, I can play them one by one.
then I call
const response = await fetch(
`${LN_API_BASE_URL}/${LN_API_VERSION}/videos?action=initializeUpload`,
requestOptions
);
const result = await response.json();
I receive upload isntructions.
await splitVideoToTemp(fileName);
await Promise.all(
instructions?.map(async (instruction, index) => {
const chunkName = `chunk_${index}.mp4`;
const outputPath = path.join(
process.cwd() + '/tmp/chunks',
chunkName
);
const blob = await getFileAsBlob(outputPath);
const result = uploadVideo(
instruction.uploadUrl,
blob,
params.pageAccessToken
);
const uploadedChunkId = await result;
if (uploadedChunkId) {
uploadedChunksIds.push(uploadedChunkId);
// delete file
await deleteFileFromTemp(outputPath);
}
})
);
all nice and well, when I upload, i receive bunch of chunk ids with each call, I put them in array, call finalize upload with obj
finalizeUploadRequest: {
video: 'urn:li:video:videoId',
uploadToken: '',
uploadedPartIds: [ .. 'ids i received', '', '', '']
}
then I poll result, its available, then I post. it success. But on the page I get one random chank from all of them.
Any Idea why? I dont get any error. Upload process is smooth. All videos go through this logic, if video short and dont need to be splitted, it uploads perfectly, but whats wrong with partial uploading? Why it post only one of the chunks and random one?
I’ll appreciate any tips