My Departement Of Nerdcore Enthusiasm

Tutorials and the like. Things which I have committed myself to. Often linux/UNIX-related.

lørdag den 8. maj 2010

Crossfade two videos clips from a command line (CLI or term)

Attention:
The video that I made these technical solutions for is now finished and can be viewed on youtube http://www.youtube.com/watch?v=uRXsp7LZaDQ

Please start by scrolling all the way down to the bottom of this post and watch the little video that is there. It will make the understanding of all this much easier.

This script can crossfade two videos from a command promote (CLI or term) in UNIX / Linux / mac. With crossfade, I mean that the first movie clip slowly tones into the next.

This command produced the example videoclip clip at the bottom of this post
./crossfade clip-a.mpg clip-b.mpg 8

I have put out the script on my homepage as a pure text file you can view it. And copy / past it to your edit or you could right-click the link and choose to download (and rename it) to your computer.

Here is the link to the script:

http://mimoart.ooz.dk/crossfade.text

HOW TO USE THIS SCRIP?

There is, as always with computers several ways to do every single thing. In this little walk-through I have chosen a one way for the sake of giving a practical example.

1st Check that you have FFmpeg and Netpbm installed (and that you are on a UNIX platform (Linux, Mac OS X, Solaris, Free BSD, etc.))

2nd Create a working directory let's just here as an example call it:

/home/mikkel/project/mymove/crossfade/

to this directory copy the two video clips that you want to crossfade (they should be in mpg format and equal in height and spread)

3rd copy / paste the text / script from the link her above to your editor and save it (pure text) as a file called "crossfade" in the same catalog.

4th Open a terminal (Xterm, rxvt, Aterm of your choice) and "cd" to the above selected directory

cd /home/mikkel/project/mymove/crossfade/

5th Make the script executable

type in the terminal window:

chmod +x crossfade

6th Choose how slow you want the transition to go. It is done with the figure at the end of command (the third argument)

in my example the figure is 8

./crossfade clip-a.mpg clip-b.mpg 8

rule of thumb is that each unit is a little more than a second. In my example here I have chosen 8 which gives me about 9 seconds in transition, you can choose a maximum of 50 which gives about a minute fading

7th Yes it is just to execute the command. In my example i write:

./crossfade clip-a.mpg clip-b.mpg 8

in the terminal window (the script can actually run from a non-X-environment as well as an X-environment terminal).

I have also made other script that uses ffmpeg and netpbm that performs video producing and video editing tasks. E. g. one to zoom in an image. One to rotate a single frame as if it was on a turntable and spun around. One for fading video clip to black. One for enhancing contrast in a very dark image etc. I'd love to send these scripts and description if anyone wants. Just write to me

contact:

http://mimoart.ooz.dk/contact.htm

Below here is notes to explain in further details.

Dependencies:
To the script there are two programs to be installed on your computer. FFmpeg and Netpbm. Netpbm is very basic software and will almost always available on your Unix / Linux machine or be easy to install. FFmpeg is planned to be compiled befor installation on your system, but it can also be found in the pre compiled version at most repositories.

Advantages of this way of crossfading video clips.

- If you have a netbook like for instance Asus eee it is much easier to work with a command then with drag and drop of the mouse because of the small screen size.

- The script can run on a very law machine without it going dead. It just takes a little longer.

- Sometimes it's more comfortable and more precise to work with text commands instead of having to sit and fiddle with the mouse

- I am use to use FFmpeg and Netpbm so for me it is a big advantage that I am not going to teach me a graphic video editing program to create this effect.

- I've tried to do the same with Kdenlive (a good graphic video editing program) on the same PC and it took much longer to render than my script takes to process. So it is faster

- It gives a much better insight into what is going on in process.


Preparation of film clips to be crossfades:
Film clips must be in mpg format which is easily done with Ffmpeg such

ffmpeg -i orig.avi -sameq clip-a.mpg

and they must have equal size for example 320 x 240

which is also easy to do with FFmpeg for example:

ffmpeg -i a.mpg -sameq -s qvga clip-a.mpg

where "QVGA" is size declaration. "QVGA" stands for the size 320 x 240

Sound
I have in this process not crossfaded sound too. I have worked with images alone and put the sound on at the end but if some want I can help to add audio processing into the script.

What happens:
The actual transition takes place at the video clips transformed to single frames. The "pamcomp" program from Netpbm takes care of merging frames two and two with a transparency mask that determines how much one image should be visible through the other. I use 30 levels of fading if it is to be a long fading for example 20 seconds, these thirty levels just spread over several frames. When this overlay process is finished for all the selected frames FFmpeg turns the frames back to mpg video again.

Before this transition process can take place some lengths of video clips must be measured. If the video clips are long (more than one minuet) it should be divided before it is write out as single frames so there will not be too many unnecessary frames in the process. This cutting is done with ffmpeg -ss and -t command and general Unix shell commands sed, expr etc. Maybe some of you will wonder why I let the script convert video clip from mpg to avi before it is measured. That is because the mpg format is not easy to measure in because it doesn't contains meta data, eg the duration and the like. avi does contain this data.

Space:
It is clear that this script requires some space on your hard disk. It writes typically around 3.000 single frames in to png images and that take up some space. In most cases 2 GB is sufficient.


2 kommentarer:

  1. hey mikkel!
    does this work correctly or is it buggy?
    I need to perform some crossfade and fadein fadeout in commandline.

    SvarSlet
  2. OMG this is one and a half year ago :-). The answer must be that it works with me, but it does not provide a stabel and proven solution. It's a bit like an old car where you need to know its quirks if you want to get it to work, but then again.. for me it works quite easily and fluently.
    However, there is something I am not satisfied with the process. There are too many times when the script is converting between mpg and avi. I would like to find a way to do this with less conversion between the two formats along the way.

    Mikkel


    SvarSlet