How to add a standard beginning and end to 182 video files using batch

Started by jmason, March 10, 2020, 10:01:31 PM

Previous topic - Next topic

jmason

Hi,
I have 182 video files to which I wish to add a standard beginning and end. I have the beginning and end as separate video files. I am trying to work out how to do this as a batch process to save time but am struggling to understand the scripting.

Any help would be greatly appreciated.

John Mason

eumagga0x2a

Basically

#!/bin/bash
OPENING_SEQ="/full/path/to/intro"
CLOSING_SEQ="/full/path/to/credits"
AVIDEMUX_CONFIG="/full/path/to/settings.py"
avidemux3_qt5 --load ${OPENING_SEQ} --run ${AVIDEMUX_CONFIG} --append=$1 --append=${CLOSING_SEQ} --save=$2


as a wrapper. Then call this wrapper with the payload as the first arg and the output file as the second one. All codec configuration goes into settings.py.

edit: fixed shebang.

eumagga0x2a

I should have added that the above is based on presumption that all videos incl. the intro video and credits have exactly the same dimensions and desirably also the same time base and fps.

dosdan

Quote from: jmason on March 10, 2020, 10:01:31 PM
I have 182 video files to which I wish to add a standard beginning and end. I have the beginning and end as separate video files. I am trying to work out how to do this as a batch process to save time but am struggling to understand the scripting.

FFMPEG is a lot quicker for this task.  There are two ways to do it: the concat demux; the concat protocol.

https://trac.ffmpeg.org/wiki/Concatenate

I tried both. With the particular MP4s I had, only the concat protocol worked correctly. (I've used the concat demux before, but not with this fileset.)

All your 182 videos must be have the same framesize, framerate and have the same number of channels and audio format in their audio streams, as must your intro and ending MP4s.  If not, you will need to split them up into batches and have a set of different intro & ending files with matching formats for each batch.

The following instructions are for Windows, but you could script this in Linux.

STEP 1. Place all the video to be top-&-tailed (but not the intro & ending clips) into a directory by themselves. In this directory, at the console prompt, type:
dir /b *.mp4  > main_videos.list (use *.mkv instead if they're in MKV containers.) 

In my testing I copy & renamed a 21s 1080p60 clip I had lying around to create 5 test video files, four of which had a space in their filename.  So, after I ran this command, here was the contents of my main_videos.txt

testfile 2.mp4
testfile 3.mp4
testfile 4.mp4
testfile1.mp4


The presence of spaces changed the sort order, but that's not important here. (If the order was important, you could edit the list after creation and move lines around.)

STEP 2. Then I copy my 1080p60 intro & ending MP4s into this directory.  Each of these was approx 5s long.

STEP 3. Finally, I ran the following batchfile from within this directory:

append_test2.bat
@echo off
setlocal EnableDelayedExpansion
cls

ffmpeg -i intro.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts -hide_banner -y intermediate1.ts
ffmpeg -i ending.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts -hide_banner -y intermediate3.ts

if not exist converted\nul md converted

for /F "delims=" %%G in (main_videos.txt) do (
ffmpeg -i "%%~G" -c copy -bsf:v h264_mp4toannexb -f mpegts -hide_banner -y intermediate2.ts
ffmpeg -i "concat:intermediate1.ts|intermediate2.ts|intermediate3.ts" -c copy -bsf:a aac_adtstoasc -hide_banner -y "converted\%%~G"
)


The concat protocol needs the files to be in .TS files so they can be concatenated together. They can be quickly and losslessly (no quality loss) converted from MP4 to TS containers. So the batchfile first needs to convert the intro & ending MP4s into temporary intermediate1.ts & intermediate3.ts files.  This is done once, near the start.

Then it checks for the presence of the converted sub-directory beneath the current directory. If this does not exist, it makes it. This is where the top&tailed videos will end up. Both the original & converted videos will remain afterwards, so you're going to need a lot of diskspace. If this is an issue, you could delete each source file as soon it was converted.

Next, a FOR /F loop reads each line in turn from main_videos.txt and, each loop, it converts the file-to-be-top&tailed to intermediate2.ts, overwriting any previous version of this file if it exists. It then concatenates intermediate1.ts + intermediate2.ts + intermediate3.ts and outputs the top&tailed file with its original filename to the converted directory. Then loops around for the next file to be processed.   

Afterwards, my converted directory looked like:
Directory of C:\test directory\Appending_Test\converted

11/03/2020  11:13 AM    <DIR>          .
11/03/2020  11:13 AM    <DIR>          ..
11/03/2020  11:17 AM        61,294,435 testfile 2.mp4
11/03/2020  11:17 AM        61,294,435 testfile 3.mp4
11/03/2020  11:17 AM        61,294,435 testfile 4.mp4
11/03/2020  11:17 AM        61,294,435 testfile1.mp4


The 21s video files are now 30s long.  Here's a converted file (my intro & ending MP4s files contain static text):

https://dl.dropbox.com/s/8esvmk2eb2nno8c/testfile1.mp4

Dan.

eumagga0x2a