»
S
I
D
E
B
A
R
«
Video Editing – MP4 and H.264 or X264
September 21st, 2009 by admin

Cut Join Edit AVC MP4 H.264 video – my little guide to getting it done as painlessly as possible

This could get a little geeky – so apologies in advance. For people with modern consumer AVC type video cameras however, this little guide could be rather useful.

I recently bought a lovely little video camera – a Sanyo Xacti HD700 see my review on GDGT. This little gem will take nice still photos (7.1 mega pixies with a 5X zoom) and rather good video (720P is fine for my TV). The camera does have a few issues (low light performance being the main one) but for the money (£109 on eBay right now) it is amazing.

Video and stills are saved to SD or SDHC cards which is fine and convenient. The video saved by the device is in ISO standard H.264 and AAC audio which is nice – and the bundled software (Premiere Elements) does a good job of whipping together DVDs and so on – but if like me you want to retain video quality and keep edited video in a decent compression format (H.264 or the open source equivalent X264), what are the options? The answer to this question seems to be impossible to find (try Googling for “mp4 cut edit” or “H.264 editor”  and see what I mean.

As my main use for this camera is to take relatively short clips over the course of a holiday, outing or special event, all of which need joining with nice dissolves and occasional trims, I need to find a way to do this as painlessly as possible. Here is my brief guide on how this can be done using open source or free software.

Tools needed

MeGui – this is a Swiss Army knife for performing all kinds of conversion and muxing magic on various video formats – it acts as as a gui or front end to many other tools

AVISynth – this is another powerful tool – acting as a frameserver for other applications

AvsP – this is a useful editing tool for AVISynth scripts

Outline of the procedure

The steps are:

  1. create an avs script to load up and dissolve the clips (optionally tweaking their colour/contrast and start/end points – AvsP is useful here but this could be done with a text editor if you are familiar with the syntax etc.)
  2. load the avs script into MeGui and use the auto feature to whack out the finished mp4 file (having selected x264 at the standard constant quality value of 25 or 26 and AAC audio at 128kbps) .

The results are brilliant and give me completed video files for my holidays etc – which I can then save to my media and backup hard drives. These have good quality despite excellent compression and are ideal as my archive format for family video files. Being ISO standard H.264 (using the x264 codec) and AAC audeo, they should remain valid as file formats for years to come and be playable on every future media device worth its salt. For more info on video files, video codecs and audio codecs, I recommend MeWki – see the notes on Containers, video codecs and audio codecs

In Detail

Install AVISynth and MeGui. AvsP does not need to be installed – it will run anywhere.

Run AvsP and start creating your script – here is an example which loads three clips and dissolves between each one in turn with an overlap of 40 frames:

A=DirectShowSource("C:\Users\Julian\Desktop\VidSource\SANY0008.MP4",audio=true)
B=DirectShowSource("C:\Users\Julian\Desktop\VidSource\SANY0009.MP4",audio=true)
C=DirectShowSource("C:\Users\Julian\Desktop\VidSource\SANY0013.MP4",audio=true)
Dissolve(A,B,C,40)

You can also tweak colour and contrast if you like – here is an example that does this:

A=DirectShowSource("C:\Users\Julian\Desktop\VidSource\SANY0008.MP4",audio=true)
B=DirectShowSource("C:\Users\Julian\Desktop\VidSource\SANY0009.MP4",audio=true)
C=DirectShowSource("C:\Users\Julian\Desktop\VidSource\SANY0013.MP4",audio=true)
out=Dissolve(A,B,C,40)
Tweak(out,hue=0.0, sat=1.3, bright=0, cont=1.1)

It is easy to perform all kinds of other actions on the clips – such as trimming and adding titles – e.g.:

A1=DirectShowSource("C:\Users\Julian\Desktop\VidSource\SANY0008.MP4",audio=true)
A2=Subtitle(A1,"Holidays June/July 2009",x=640, y=300, first_frame=0, last_frame=90, + \
 font="Arial", size=38, text_color=$DDDDFF, halo_color=$000000, align=8, spc=0, lsp=0)
B=DirectShowSource("C:\Users\Julian\Desktop\VidSource\SANY0009.MP4",audio=true).Trim(0,4500)
# previous line trims the clip to 5 minutes (4500 frames at 25fps)
C=DirectShowSource("C:\Users\Julian\Desktop\VidSource\SANY0013.MP4",audio=true)

and so on – best to have a look at the AVISynth site for more details

So now we have a script that will load the clips ready for processing by MeGui – so open up MeGui and in the Video and Audio sections browse to the script you have just created.

A little configuration is required:

Encoder settings
For video I start with x264 scratchpad profile and change the quality setting to constant quality at 25. Select the mp4 file format (remember with videos you have containers such as avi, mp4 and so on, and the video inside these containers will be compressed using a codec such as DivX, Xvid, H.264 alongside audio which will be in wav, mp3, AAC, AC3 – read it up on Wikipedia if this needs clarifying).

On the audio – select FAAC scratchpad and set the bitrate to 128 ABR . On the audio extension, choose MP4-AAC

Now all that is necessary is to hit autoencode – it will whir for a while, then offer to add the jobs to the queue. Once queued, go to the queue tab and Start.

If everything has gone right, you should end up with a nicely edited single file in standard ISO MP4 format with all your clips nicely dissolving into each other.

It would be nice to find something that would do this out of the box, but until then, the above is not too onerous.

Now a little problem

The above technique will only work for a limited number of files needing to be joined. Your mileage may vary – but on my PC equipped with 4Gb of memory, it crashes after around 15 or more video clips. Why? – the answer seems to be that AVISynth needs to load the codec dlls once for each clip – so the more clips, the more memory this eats up and this causes out of memory errors. So how do we get around this problem? My simple answer is to join (with dissolves, trims, titles, colour tweaks etc.) the source videos in batches of 10 at a time, using a very high bitrate to preserve quality as much as possible (e.g. CQ setting of 15 and audio bitrate 192kbps), and then run a final join of the resulting files using the desired final quality and bitrate. The results are, to my eyes, indistinguishable from videos created in a single join process.

Here are some example AVISynth scripts which illustrate all of the above:

Example 1 – join up in batches of 10 clips at a time

Aa=DirectShowSource("C:\Users\Julian\Desktop\VidSource\FILE01.MP4",audio=true)
A=Subtitle(Aa,"Lefkada June/July 2009",x=640, y=300, first_frame=0, last_frame=90, + \
font="Arial", size=38, text_color=$DDDDFF, halo_color=$000000, align=8, spc=0, lsp=0)
B=DirectShowSource("C:\Users\Julian\Desktop\VidSource\FILE02.MP4",audio=true)
C=DirectShowSource("C:\Users\Julian\Desktop\VidSource\FILE03.MP4",audio=true)
D=DirectShowSource("C:\Users\Julian\Desktop\VidSource\FILE05.MP4",audio=true)
E=DirectShowSource("C:\Users\Julian\Desktop\VidSource\FILE06.MP4",audio=true)
F=DirectShowSource("C:\Users\Julian\Desktop\VidSource\FILE07.MP4",audio=true)
G=DirectShowSource("C:\Users\Julian\Desktop\VidSource\FILE08.MP4",audio=true)
H=DirectShowSource("C:\Users\Julian\Desktop\VidSource\FILE09.MP4",audio=true)
I=DirectShowSource("C:\Users\Julian\Desktop\VidSource\FILE10.MP4",audio=true)
J=DirectShowSource("C:\Users\Julian\Desktop\VidSource\FILE11.MP4",audio=true)
out = Dissolve(A,B,C,D,E,F,G,H,I,J,40)
Tweak(out,hue=0.0, sat=1.3, bright=0, cont=1.1

Once you have the resulting files, join them up using the final desired quality and bitrate:

A=DirectShowSource("C:\Users\Julian\Desktop\VidSource\Full1-muxed.MP4",audio=true)
B=DirectShowSource("C:\Users\Julian\Desktop\VidSource\Full2-muxed.MP4",audio=true)
C=DirectShowSource("C:\Users\Julian\Desktop\VidSource\Full3-muxed.MP4",audio=true)
D=DirectShowSource("C:\Users\Julian\Desktop\VidSource\Full4-muxed.MP4",audio=true)
Dissolve(A,B,C,D,40)

And that gives you the finished long video all nicely edited with cross dissolving transitions between each clip.


.Trim(

Leave a Reply

»  Substance: WordPress   »  Style: Ahren Ahimsa