pastebin

Private paste #67863: Shell script to concatentate mpg videos with fade-to-black transitions

#!/bin/bash

declare -a MOVIE_FRAMES

if [ -z "`echo $@`" ]; then
	cat << EOF

Usage:
        $0 input1.mpg input2.mpg [...]

The following environmental variables matter:
        FADE_FRAMES        The number of frames to fade on
                             each side of each splice
                             [DEFAULT=50]
        BLACK_PICTURE_FILE The filename of your all-black
                             image file.
                             [DEFAULT="black.png"]
	OUTPUT_FILE        The output filename.
                             [DEFAULT="output.mpg"]

Caveats:
        This script makes no attempt to make sure that
        your input files are '.mpg', that they have the
        same frame rate, that they are the same size, or any-
        thing else. You *must* do this yourself. You will
        need ffmpeg patched with the pip vhook filter for
        this to work. You will also need to a solid still
        image file with the same dimensions as your videos (see
        BLACK_PICTURE_FILE above). JPG, PNG, GIF and probably
        some other formats are fine. You should probably
        rewrite all of this, especially the text processing on
	line 57.
EOF
exit
fi

if [ -z "$BLACK_PICTURE_FILE" ]; then
	BLACK_PICTURE_FILE="black.png"
fi

if [ -z "$FADE_FRAMES" ]; then
	FADE_FRAMES=50
fi

if [ -z "$OUTPUT_FILE" ]; then
	OUTPUT_FILE="output.mpg"
fi

COUNT=1

cat << EOF


 Determining number of frames each input movie.
 This could take some time. The only way to count
 the frames is to read the entire file, transcoding
 the video is a slow but convenient way to do this
 from a shell script. It would probably be easy to
 write a frame counter with libavcodec or libavfilter.


EOF

#Get the lengths and frame rates of the movies using ffmpeg
for movie in $@; do
	ffmpeg -i $movie -qscale 255 temp_testlength.mpg 2> temp_testlength
	MOVIE_FRAMES[$COUNT]=`echo \`cat temp_testlength | tr "\015" "\012" | grep frame= | tail -n 1 | cut -d = -f 2 | cut -d 'f' -f 1\``
	rm -f temp_testlength.mpg
	COUNT=$(($COUNT+1))
done

rm -f temp_testlength

COUNT=1

#Make one big movie
cat $@ > temp_movie.mpg

COUNT=1
TOTAL_FRAMES=0

#We need to build our command into a string because we don't know how many vhooks we'll need
FFMPEG_COMMAND="ffmpeg -i temp_movie.mpg "

#Insert the transitions into the ffmpeg command
for movie in $@; do
	FFMPEG_COMMAND="$FFMPEG_COMMAND -vhook '/usr/lib/vhook/pip.so -f "$BLACK_PICTURE_FILE" -t FFFFFF -x 0 -y 0 -s $((${MOVIE_FRAMES[$COUNT]}+$TOTAL_FRAMES-$FADE_FRAMES)) -e $((${MOVIE_FRAMES[$COUNT]}+$TOTAL_FRAMES+$FADE_FRAMES)) -d $FADE_FRAMES -m 0' "
	TOTAL_FRAMES=$(($TOTAL_FRAMES+${MOVIE_FRAMES[$COUNT]}))
done

FFMPEG_COMMAND="$FFMPEG_COMMAND -sameq \"$OUTPUT_FILE\""

echo Here\'s the command we\'re running:
echo
echo $FFMPEG_COMMAND
echo
echo

#Run the command!
bash -c "$FFMPEG_COMMAND"

if [ "$?" -eq 0 ]; then
	echo
	echo Success! Output file is: $OUTPUT_FILE
	echo
else
	echo
	echo Failure. ffmpeg could not create video.
	echo
fi

rm -f temp_movie.mpg

New paste

  Language:
Private   — Wrap long lines  —  1 + 2 =   —  

About this pastebin

Welcome to the bulix.org / pastebin. Please don't use this pastebin for illegal purposes, defamation or kitten-squashing.

This pastebin is written using PHP and MySQL and relies on Alex Gorbatchev's syntax hhighlighter (JavaScript based). To avoid spam, you will be required to complete a small mathematical challenge when adding a new paste.

New! Try the pastebin command-line tool: paste.py (requires Python and python-beautifulsoup).

30 more recent public pastes


# Title Type Date
1 Untitled C paste by 77.104.192.100 C 02 Sep 2010, 16:52
2 Untitled Visual Basic paste by 187.7.138.176 Visual Basic 02 Sep 2010, 16:36
3 Untitled Ruby paste by 217.9.0.179 Ruby 02 Sep 2010, 15:42
4 whywhywhy C 02 Sep 2010, 15:15
5 Untitled Javascript paste by 134.184.43.109 Javascript 02 Sep 2010, 14:41
6 Untitled Bash paste by 158.42.126.237 Bash 02 Sep 2010, 14:32
7 Untitled C paste by 184.75.34.234 C 02 Sep 2010, 14:24
8 Untitled Perl paste by 155.253.6.151 Perl 02 Sep 2010, 14:22
9 Untitled Ruby paste by 58.215.81.151 Ruby 02 Sep 2010, 14:14
10 Untitled SQL paste by 98.190.244.179 SQL 02 Sep 2010, 13:55
11 Untitled C paste by 85.18.126.106 C 02 Sep 2010, 13:40
12 Untitled C++ paste by 187.111.15.230 C++ 02 Sep 2010, 11:00
13 Untitled SQL paste by 85.254.213.163 SQL 02 Sep 2010, 10:58
14 Untitled Ruby paste by 89.232.63.173 Ruby 02 Sep 2010, 10:47
15 Untitled Visual Basic paste by 74.50.59.175 Visual Basic 02 Sep 2010, 09:40
16 Untitled Perl paste by 202.14.181.11 Perl 02 Sep 2010, 09:04
17 Untitled PHP paste by 61.181.246.205 PHP 02 Sep 2010, 08:52
18 Untitled Bash paste by 174.138.160.29 Bash 02 Sep 2010, 08:43
19 AdultFriendFinder Report ASCII 02 Sep 2010, 06:56
20 Untitled PHP paste by 183.91.87.16 PHP 02 Sep 2010, 03:56
21 Untitled Javascript paste by 81.227.80.35 Javascript 02 Sep 2010, 03:21
22 Untitled Lua paste by 218.201.21.182 Lua 02 Sep 2010, 01:58
23 Untitled C# paste by 79.98.27.109 C# 02 Sep 2010, 01:41
24 Untitled SQL paste by 200.184.84.133 SQL 02 Sep 2010, 00:41
25 Untitled C# paste by 173.61.246.36 C# 01 Sep 2010, 23:24
26 Untitled Visual Basic paste by 121.135.192.120 Visual Basic 01 Sep 2010, 23:07
27 Untitled C# paste by 190.30.88.228 C# 01 Sep 2010, 21:05
28 Untitled Css paste by 85.9.50.182 Css 01 Sep 2010, 19:53
29 Untitled C paste by 65.46.53.202 C 01 Sep 2010, 19:41
30 Untitled Ruby paste by 201.192.75.42 Ruby 01 Sep 2010, 18:44

Powered by the Bulix.org Code Pastebin, by Maxime Petazzoni. View pastebin statistics.