Blog-Archiv

Mittwoch, 30. März 2022

Combine Scroll Text Videos with ffmpeg

In my latest Blog articles I presented shell scripts to scale an image to a certain size, and to scroll a multiline text over a background image. Now let's combine these two scripts and generate a video from several text files with associated background images.

As said, you need ffmpeg and ImageMagick installed on your machine.

Preparations

Text files and background images must be associated by following naming convention:

  1. A text file must have the extension ".txt"
  2. The associated image must have the extension ".png" (can be changed in script line 6)
  3. The intended sequence of the files must be expressed by alfabetical sort order of the files (so best prepend "99_" before any file name and change 99 to the intended order number)
  4. The base name of the text file (without extension) must be the same as that of the image,
    thus a text file named 01_example.txt will be associated with the image file 01_example.png.

Here is an example for a sequence of 3 video clips with different background images, to be combined into one result video ALLCUTS.MP4:

01_putin-demagogy.png
02_kiev-destruction.png
03_bombed-flat.png
01_putin-demagogy.txt
02_kiev-destruction.txt
03_bombed-flat.txt

Script

  • joinScrollTextVideos.sh
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#################################################
# create and join videos where texts scroll over images
#################################################

allCutsVideo=ALLCUTS.MP4
imageExtension=png

currentDir=`pwd`
cd `dirname \$0`    # change to where this script resides
PATH=$PATH:`pwd`    # take sibling scripts into path
cd $currentDir

syntax() {
    echo "SYNTAX: $0 directory [widthxheight]" >&2
    echo "    Creates and joins videos where texts scroll over images." >&2
    echo "    Text- and image-files have same name but different extensions: .txt -> .$imageExtension" >&2
    echo "    directory: the folder where text and image files are" >&2
    echo "    widthxheight: image target dimension, default is 1920x1080" >&2
    echo "    CAUTION: file names must not contain spaces or any kind of quotes!" >&2
    exit 1
}

[ -d "$1" ] || syntax
widthxheight=$2

cd $1

concatFile=concat.txt
rm -f $concatFile 2>/dev/null

for textFile in `ls -1 *.txt | sort`
do
    echo "Checking $textFile for associated image ...." >&2
    baseName=`basename "\$textFile" .txt`
    imageFile="$baseName.$imageExtension"
    
    [ -f "$imageFile" ] && {    # when there is an associated image for text
        echo "Found $imageFile ...." >&2
        
        scaledImageFile="scaled_$imageFile"
        imageToSize.sh "$imageFile" "$scaledImageFile" $widthxheight || exit 2
        
        videoFile="$baseName.MP4"
        scrollTextOverImage.sh "$scaledImageFile" "$textFile" "$videoFile" || exit 3
        
        rm -f "$scaledImageFile"
        
        echo "file $videoFile" >>$concatFile
    }
done

[ -f $concatFile ] || {
    echo "Found no associated .txt and .$imageExtension files in `pwd`" >&2
    exit 4
}

echo "Joining videos ...." >&2
ffmpeg -v error -y -f concat -i $concatFile -c copy $allCutsVideo

rm -f $concatFile

echo "Done." >&2

Line 5 defines the name of the resulting video.

Line 6 defines the extension of image files. Change it to "jpeg"or any other extension of your images that ImageMagick supports.

Lines 8 to 11 serve to take sibling scripts into the execution path. Make sure that the scripts imageToSize.sh and scrollTextOverImage.sh are beside joinScrollTextVideos.sh (see my recent articles for their source code).

Line 13 to 21 provides syntax documentation for erroneous calls.

Lines 23 to 29 pick up the call parameters and change to the given directory. The concatFile is needed for ffmpeg, the script removes it in case it exists.

Lines 31 to 50 loop over the sorted list of .txt files in the working directory. First the base name association is done by removing the .txt extension (line 34) and building the according image file name. If that image file exists (line 37), the script will generate a temporary 1920x1080 image from it (line 41) and combine that with the text file (line 44). The resulting video clip is appended to the concatFile as preparation for the final ffmpeg call to join all clips.

Lines 52 to 54 check whether there were videos created and exits when not.

Line 58 finally joins all video clips to one result video called ALLCUTS.MP4, here the concatFile is passed to ffmpeg. The concatFile gets removed afterwards.

Resume

This script lets produce videos quite quickly. Just write your texts, give the files an alfbetical order, choose images and give them the same base name as the text files. Everything else will be done by ImageMagick and ffmpeg.




Keine Kommentare: