Wednesday, 19 August 2015

Gluing a chunked video back together

Yesterday I tried downloading a replay of a video hosted on periscope. The video was delivered in the form of hundreds of chunks. After saving these files, the question was how to concatenate them back together into a single video file that I could watch later at my leisure. Quite possibly there is a full version of the video hosted at the same address the chunked files come from, but if there was, you'd need to know the filename.

I found that ffmpeg offers two different ways of concatenating video files together. Because I'm on windows, getting the list of files is rather tricky. Each chunk is named like 'chunk_38.ts'. What I ended up doing was creating a spreadsheet, and filling a cell with the first number (38 in my case), and next to this I added a cell with a formula to make the filename and output needed for ffmpeg CONCATENATE("file 'chunk_", A1, ".ts'"). Then I dragged these cells down via copy until I reached the number of the last chunk.

With that done, I copied the cells and pasted them into a plain text file. Then I just had to run the ffpmeg demuxer and point it to that file. Unfortunately this took a long time and didn't work well. I ended up with a video that said it was 2s long, but was actually much longer. But the video image stayed at the start. (I'm not sure about the audio as my sound wasn't working).

So then I tried the second ffmpeg option, which requires putting all the files into a single command. Using find and replace in the text editor, I replaced the new lines / carriage returns with the pipe character needed to separate the filenames for the ffmpeg command. But when I pasted the command into the command prompt, it was too many characters.

Looking it up, it seems there is no way to get around this limitation, the advice from MS is to modify your program so that it can read the parameters from a file rather than the command line. So I tried doing as many of the chunks as I could fit into a single command. Then I tried concatenating the result of the first concat with as many more of the chunks as I could fit into a second command. But this didn't work and I just got an error.

So I found a perl script for renaming files, and modified that to rename all of the files to just their number (with no file extension). That was the only way I could fit them all into a single command that wasn't too long for windows. I ran the command, and it created the video okay.

Today I fixed the problem with my sound card. I just had to uninstall the device, then reinstall the drivers. Why on earth it stopped working, I have no idea.

But I found that the video I created from the chunks had no sound (despite ffmpeg giving data on the total amount of audio copied to the output file as well as video). So searching the web again for another alternative way of concatenating files together in windows, I came across the copy command. Using this still had the same limits of needing the filenames to all fit within a single command. But it worked nicely, and very quickly.

So if you need to concatenate a chunked video back together in Windows, the copy command seems to be the best way to do it.

No comments: