Linux Notes: Video Editing

Last modified: 05/11/2008

Contents

Video Editing Process
Importing Video
Disk Space Estimates
Kernel 2.6.15 Firewire (1394) Hotplug
Import video with Kino
Importing video with dvgrab
Importing video with dvconnect
Importing live TV
Determining file type
Pre-processing
Cut/Splice AVI Files
Media Resolution
DVD Authoring
DVD Production Flow
Editing Video with Cinelerra
Building and Maintaining Cinelerra
Output Dimensions
DVD
YouTube
Use Clips
Fragmented AVI files
Subtitles and Credits
Other Cinelerra Tips
Producing Quicktime Movies
Producing AVI Movies
Exporting clips to Mac OS
Syncing Audio
Exporting to Camcorder
Sending Raw DV with dvconnect
Batch Conversion Recipes
Determine file type
Convert Quicktime to AVI
Demux Quicktime to AC3 and MPEG 2 (VOB) for DVD
Convert AVI to Quicktime/MPEG-4 and strip audio
Extract audio from AVI to WAV
Convert AC3 48 KHz audio to WAV 44.1 KHz
Extract portion of VOB file to MPEG and strip audio
Downsampling video by dropping frames
Summarize movie times
TV Tuner/Video Capture Cards

Video Editing Process

  1. Import video to hard disk.
  2. Editing video.
  3. Write out to Quicktime file, DVD, or video tape.

Importing Video

Disk Space Estimates

My actual experience with Canon GL-1 (format: digital video), record mode: SP, audio mode: 16 bit: See LVM, Growing a partition

Kernel 2.6.15 Firewire (1394) Hotplug

Not much to it, really.

1. Insure that /lib/modules/kernel/modules.dep contains: ieee1394, ohci1394, raw1394, dv1394, video1394. If not, compile and install these kernel modules.

2. Insure that /lib/modules/kernel/modules.ieee1394map contains raw1394, dv1394, and video1394. If not, figure out why not.

3. Add these to /etc/modules.autoload.d/kernel-2.6 so your Firewire (1394) card is recognized (otherwise, the kernel won't care about events sent from the card):

# For IEEE-1394 (Firewire) bus.
ohci1394

4. Create file /etc/modules.d/ieee1394 (you can name it anything) and put in these driver dependencies:

# Specials for ieee1394 devices.

# Video camera:
below raw1394 dv1394
below video1394 raw1394

5. Reboot to start fresh. Or, you can remove all 1394 modules with modprobe -r, load ohci1394 with modprobe ohci1394, and run modules-update to incorporate your new modules.d file.

When you plug in the camera, the raw1394 module will load automatically, and will force dv1394 to load before it. If you do not have ieee1394, ohci1394, dv1394, and raw1394 all loaded, you will have problems.

When the camera is unplugged, raw1394 is not unloaded. The system appears to consider the camera's ieee1394 node in a "suspended" rather than "gone" state. The modules uses a small amount (about 20Kb) of kernel memory, and you can unload them manually with modprobe -r dv1394, though this may also unload ohci1394, too, and then hotplug will no longer work.

Debugging hotplug: Uncomment the "DEBUG=yes" line in /etc/hotplug/ieee1394.agent. Plug camera in. Look for messages in /var/log/messages.

Hibernate: Well, this all sounds good, but appears to not work with hibernate. Use this script to set things right:

#!/bin/bash
# Reset kernel modules for IEEE-1394 needed to access Canon GL1 camera.

# Remove all ieee1394 modules
modprobe -r video1394
modprobe -r dv1394
modprobe -r raw1394
# Load in correct order
modprobe dv1394
modprobe raw1394

Import video with Kino

Kino is gradually acquiring the same capabilities as dvgrab, though dvgrab may remain more capable. Try Kino first, as you can more easily see results.

Importing video with dvgrab

dvgrab is recommended for capture from IEEE-1394 (Firewire). Made by the Kino people (http://kino.schirmacher.de), dvgrab is a command line utility. These instructions also apply to Kino.

Normally, you can just insert a tape in your camera and start dvgrab. It will find your camera and control the playback.

Here's what worked with my camera:

  1. Connect camera to computer with Firewire cable. Turn camera on in VCR mode. Insert tape and position at beginning of segment.

  2. Use lsbus ieee1394 to find the camera's GUID. The Canon GL-1's is 0x00008500001426B6. Use dvgrab 1.8 with the --guid 0x00008500001426B6 option. Might work fine with other cameras without this option. Earlier versions do not work well with automatic camera control (AV/C), so also use the --noavc option. The minimum version of dvgrab which worked with this camera was 1.4.

  3. For recording longer than 4m40s (~1 Gb), use the --autosplit option. It will split the input files at around 1 Gb and also when the recording was stopped and restarted (nice!).

  4. Supply a base name of the form: summer-vacation- (note trailing hyphen)

  5. Start dvgrab with these options.

  6. Recent versions of dvgrab will automatically start the camera playback. If not, start the camera (from stop, not pause). dvgrab will detect that the camera has started playing and will begin capturing. When done, stop dvgrab with Ctrl-C (it won't stop if you stop the camera, though will not store blank frames on disk).

dvgrab produces AVI files. These may be used directly by either Kino or Cinelerra, and may be viewed with mplayer.

Importing video with dvconnect

dvconnect can capture video using the video1394 driver, though it does not split the source into scenes, or limit the capture file size, as dvgrab can.

Unlike dvgrab, dvconnect starts capturing as soon as it is started, and stops when playback stops. Start the dvconnect before starting the camera playback. Stop dvconnect with Ctrl-C after the camera is stopped.

dvconnect produces Raw DV files. These may be viewed with playdv or mplayer.

Run playdv like this on my system because's default display produces a black picture:

playdv -d 1 file

Importing live TV

Use TVTime to view the source material and to adjust the picture.

nice -n -20 \
    mencoder -v tv:// \
      -tv driver=v4l2:device=/dev/video0:input=1:normid=5:
          brightness=43:contrast=35:saturation=50:hue=56:
          outfmt=yuy2:amode=1:alsa:forceaudio \

      -vf-add harddup \
      -ovc lavc -lavcopts vcodec=mjpeg:vhq \
      -noautoexpand \
      -oac pcm -srate 48000 \
      -endpos "$1" \
      -o "$2"

All options to -tv must be joined together. The above command captures interlaced video from a video card receiving PAL-M (Brazil). A good VCR which can play video tapes other than NTSC is the Samsung SV-5000W (tip: while playing the tape, change the "Play System Mode" to something other than "auto" to prevent the VCR from choosing a different standard in poorly encoded sections).

The above command captures interlaced video. It does not look to good in Cinelerra, but today the end result is a DVD played on an NTSC monitor. NTSC expects interlaced video, so I might as well put interlaced on the DVD, and capture it that way, too. Otherwise, I would have to(imperfectly) generate progressive images from the interlaced only to have them converted back again.

See this for a comparion of transmission standards. See Video Interchange for lots of tech info and conversion services.

Options:

To see v4l2 options, run:

mencoder -v -v -v tv:// -tv driver=v42l:device=/dev/video0

input=1 1 = Composite1 -- see v4l2 options
normid=5 5 = PAL-M -- see v4l2 options
brightness, contrast, saturation, hue Copy from TVTime
outfmt=yuy2 YUV encoding -- see v4l2 options.
amode=1 1 = stereo
alsa Capture through ALSA
forceaudio TV card does not export audio to ALSA, and instead is wired to the sound card. forceaudio tells mencoder to capture sound even though v4l2 driver reports no sound is available.
-vf-add harddup Forces duplicate images to be output; better for reprocessing
-ovc lavc Use libavcodec
vcodec=mjpeg Use libavcodec's Motion J-PEG encoder. Although it uses more disk space,this produces better images than MPEG-4. Also, Cinelerra can read MJPEG, but not MPEG-4.
vhq Configure high-quality encoding parameters.
Disk space: 2.42 MB/sec
-noautoexpand ?
-oac pcm
-srate 48000
Encode sound in PCM format (as opposed to lossy MP3), and capture at sound card's native sample rate.
-endpos HH:MM:SS How long to capture for
-o file Output filename

Trouble options:

Unnecessary options:

Mencoder status line:

A-V:0.000 A/V sync offset, according to encoder.
[vvvv:aaaa] Average bitrate of video and audio, in Kbits/sec. Also shown in final summary.
A/Vms milliseconds/buffer for audio/video. Example: 270/14 means 270 msec to capture an audio buffer, and 14 msec to capture a video buffer (not clear how large a buffer is).
D/B/S Duplicate frames/Bad frames/Skipped frames

Determining file type

Determine file type with transcode (also see Transcode Recipes):

tcprobe -i file

Pre-processing

Transcode is good for batch pre-processing before the editing begins. However, it's a pain to figure out. Here are some recipies.

Cut/Splice AVI Files

Someone asked for a copy of some video you shot. You want to give them something as close to native format as possible so they can edit it themselves. Most likely, that means Micro$oft's AVI format. Use:

Media Resolution


Media
Resolution (NTSC)
VCD
352x240
SVCD
480x480
DVD
720x480
DV (Digital Video)
720x480

Set the the video editor to match the output dimensions early in the editing process. Otherwise, the output may be clipped unexpectedly if the dimensions are changed later, or are altered with transcode in batch mode.

DVD Authoring

See:
Notes from the above:

DVD Production Flow

  1. Edit movie in Cinelerra with resolution settings:
    Audio Sample Rate: 48000
    Channels: 2 or more
    Frame Rate: 29.97 (NTSC)
    Width: 720
    Height: 480
    W Ratio: 1
    H Ratio: 1
    Color model: YUVA-8 bit
    Aspect Ratio: 4:3 or 16:9 (wide screen)
    Interlace: Top fields first (NTSC video is interlaced)
  2. Render video and audio separately:
    Video: YUV2MPEG Stream, choose the "DVD" options from both the mpeg2enc and ffmpeg menus. Modify the resulting command by removing the -hq option (not supported by ffmpeg despite what man page says). Also, ffmpeg has an interlace bug and a fixed version is not yet available as an ebuild, so consider using mpeg2enc instead. Use file suffix .m2v and do not use spaces in your pathname. Run file on your output and you should see something like this:
    MPEG sequence, v2, MP@ML interlaced Y'CbCr 4:2:0 video, CCIR/ITU NTSC 525, 4:3, 29.97 fps
    Audio: AC3 Audio, 384 kbits. Use file suffix .ac3
    (MPEG Audio seems to be having problems -- see Cinelerra.)
    Run file on your output and you should see something like this:
    ATSC A/52 aka AC-3 Dolby Digital Stream, 48 kHz, ... 384 kbits/s
  3. Verify that output can be viewed and heard with xine.
    Play both together with gmplayer -audiofile file.ac3 file.m2v
  4. Merge both with mplex (this step may be skipped if using dvdstyler):
    mplex -f 8 -o file.mpg file.m2v file.ac3
    or:
    tcmplex -i file.m2v -p file.ac3 -o file.mpg -m d
  5. Create a simple DVD in the DVD subdirectory with:
    dvdauthor -o DVD file
  6. Test with:
    xine dvd://path/to/DVD/VIDEO_TS
    xine dvd:/
    full/path/to/dvd.iso
  7. (Optional) Create ISO from DVD directory:
    mkisofs -dvd-video -udf -o dvd.iso DVD/
    Other useful options (these work with growisofs, too):
    -speed=nBurn speed
    -publisher publisher_dataInsert publisher data into DVD header. Upto 128 characters.
    -p preparer_dataInsert preparer data into DVD header. Upto 128 characters.
    -V volume_idSpecifies volume ID (or volume label). Upto 32 characters.
    -volset volset_dataSpecifies volume set (what's this for?). Upto 128 characters.
  8. Burn DVD with:
    growisofs -dvd-compat -Z /dev/dvd -dvd-video DVD/   using DVD dir
    growisofs -dvd-compat -Z /dev/dvd=dvd.iso         
      using ISO image
    where -Z device specifies your DVD burner.
    growisofs advises using a speed half or less than the rated media burn speed (cheap DVD players cannot handle the marginal write errors that occur with high-speed burning).
  9. Eject: eject /dev/dvd
Additional notes on DVD burning:

About burn speed. The maximum speed for my DVD burner is 12. Writing 3.577 GB to several DVDs, this is what I got:

Write Speed Actual average speed
Maximum (12x) 6.3x
speed=8 7.3x

Is faster better? Apparently not.

This guy has a utility which displays how many errors are encountered on a disc. Only works for recent Plextor drives. It suggests that writing at a drive's maximum speed produces more errors. That was for a Plextor. Not sure how that relates to your drive. Plextors have a good reputation, though.

QPxTool is a new utility for Linux that works for several types of drives. Appears to not work on mine, though.

MfgrMfgr IDResults
TDK TTG02Problems reading in some consumer players.
MemorexCMC MAG. AE1Works well in consumer players.
MaxellTYG02Works well in consumer players (Taiyo Yuden)

All media burned reliably and were readable in other computers and professional studio equipment. The problem was with consumer players.

Editing Video with Cinelerra

I use Cinelerra to edit video. It allows multiple video and audio tracks, and separation of video from audio. Kino does not (though simpler is sometimes better).

Building and Maintaining Cinelerra

I found that Gentoo packages Cinelerra were released too infrequently for my use, and the best stuff was in the CVS code repository at cvs.cinelerra.org. Keeping the code running was surprising difficult. It broke often because I upgraded other packages on my system rather oftern, and Cinelerra depends on many other libraries. See my build notes.

Output Dimensions

Know your output dimensions before you start. If you need to crop after editing, you'll be sorry.

DVD
YouTube

(From You Tube's answer page)

Limits: 100 MB or 10 minutes, whichever limit is met first.

(From You Tube's answer page)

From cinelerra mailing list:

Render as for DVD, then:

ffmpeg -i file.mpeg -b 600k -s 320x240 -acodec mp3 file.small.mpeg

Use Clips

Cinelerra allows only one audio/video sequence to be edited at a time. Clips allow you to save a switch between different bits of work.

Fragmented AVI files

Sometimes dvgrab fragments the video into many small pieces. Cinelerra can piece these together into "clips". Clips are not files, but descriptions of how to piece together fragments of files so they appear to be a single piece (logically concatenated). The leader and trailer of the clip may be shortened before committing.

  1. View Resources, select "media".
  2. Identify short sequential segments that belong together.
  3. Starting with the first one, append together in the main window.
  4. Adjust In and Out points.
  5. Click the "Clip" icon and name it. Unlike media, comments may be added to clips (version 1.1.7).
  6. After saving as a clip, the fragments can be removed from the main window.

It is best to join together fragments into clips before actual editing begins.

Also: AVI files may be merged together before beginning editing with avimerge

Subtitles and Credits

Cinelerra supports simple subtitles and scrolling credits. Use the "Title" video effect. Limitations: in any one title, typeface family, size, and color must all be the same; scrolling credits are limited in size (to what?), pictures may not be included, and advanced page layout is not supported.

For advanced titles, create a PNG image with The Gimp and import into Cinelerra as a resource. Tips:

Other Cinelerra Tips

Producing Quicktime Movies

Cinelerra supports "Quicktime for Linux", which apparently is not exactly the same as Apple's Quicktime, but sometimes is compatible. Files that Xine or mplayer can read may not be readable by Apple's Quicktime. I installed Apple's Quicktime for Windows using wine.

These codec combinations worked for both the Xine and Quicktime players.

Audio:

Video:

2007-Jan: Quicktime for Linux does not work with Apple's Quicktime viewers. Tried versions 6.5.2 and 7.1.5 of Quicktime for Windows. Not clear why, as some older movies that did work no longer do, yet some other people's movies do. What works is converting Cinelerra's output to MPEG-4:

ffmpeg -i input.mov -sameq output.mpg

Also See: Cinelerra Community Center, Cinelerra Tutorial, Cinelerra TWiki On-line Manual, Kino

Useful links:

Producing AVI Movies

Cinelerra doesn't do a good job creating AVI files. Sometimes it works, and occasionally the target platform has trouble. Create output as for Quicktime and then convert to AVI with mencoder:

mencoder -o file.avi -ovc lavc -oac lavc
    -lavcopts acodec=mp3:vcodec=msmpeg4vs:vbitrate=6000
    -srate 48000 file.mov

Exporting clips to Mac OS

Use AVI to export clips to Mac OS. Settings:

Audio:

Video:

Syncing Audio

Videos with background music usually sound bad because the music is not recorded well by the camera's microphone. If you have a copy of the background music, it can be mixed into the video during editing. Don't replace the recorded audio, as it contains important ambient sound.

Background music from a CD player is an ideal audio source because accurate digital clocks insure distortion-free audio. The CD audio can be ripped and then easily inserted into the movie editor and aligned with the recorded audio. You'd think so, anyway. In practice, I have found 2 minute songs which are synced at the beginning of the song and out of sync at the end. The cause is either a too fast or too slow CD audio player, or a too fast or too slow camcorder, on the order of 0.2%. Solution: stretch or shrink the ripped audio to fit the video recording. Procedure:

  1. Align the beginning of the audio.
  2. Near the end of the song, estimate the amount of lead or lag. You might have to realign the audio track and then measure the difference.
  3. Calculate the percentage the audio track needs to stretch or shrink:
      100 * (actual-length + lead)/actual-length
  4. Stretch audio with sox. For example, to shrink by 0.2%:
      sox original.wav outfile.wav stretch 0.998
  5. Insert modified audio and align with recorded audio.
Alignment: The Cinelerra docs claim a track can be shifted with the right mouse button. For mysterious reasons, it doesn't always work. In any case, it does not give good feedback to the user for audio tracks. Sometimes it is useful to edit the project XML directly. Procedure for aligning audio tracks:
  1. Locate a clear and sharply rendered feature in the original audio track (e.g. spoken letter "T" or drum beat). Locate the same feature in the music track. Measure the distance, t, between them.
  2. Save and quit Cinelerra.
  3. Open project XML file and locate <TRACK> containing <FILE SRC=your-source-file>. Now locate the STARTSOURCE= value of the containing <EDIT> tag.
  4. If feature occurs later in the music track, the track must be advanced (i.e. started later). Convert t to samples (CD audio sampling rate is 44.1KHz) and add to STARTSOURCE. If stereo, add to both tracks.
  5. Save file, restart Cinelerra and load the project.
It's useful to write down previous STARTSOURCE values and to loop playback over the feature.

Exporting to Camcorder

Sending Raw DV with dvconnect

If you imported the video using dvgrab, then the images are compressed in DV format (within an AVI file). DV is a lossy format, and it is best to avoid re-encoding it. For the most part, Cinelerra avoids re-encoding source frames, but does so for frames which are mixed or overlayed. Someone out on the Internet suggested that this leads to a perceptable shift in video quality, and can be avoided by forcing Cinelerra to re-encode everything by mixing in an empty video track. If the destination is VHS, I don't think it's a problem.

  1. Due to the difficulty in synchronizing equipment, it's best to include a leader and trailer on your movie, either a solid color or a static image.
  2. When rendering a large movie, divide the output into segments at most several minutes in length. Do so by inserting labels on the movie timeline and using the renderer's "Create new file at each label" option. This allows you to re-render unsatisfactory portions without having to re-render the entire movie. Name the output file something with two zeros in it, and Cinelerra will replace the "00" with incrementing numbers.
  3. Render from Cinelerra directly to DV RAW.
  4. Review output with totemmplayer, or playdv (playdv comes with libdv, but the Gentoo package may not install it anymore). If it doesn't look good now, it won't look good on tape.
  5. Join DV RAW segments together with cat. If you don't, dvconnect may introduce audio drop-outs between the segments.
  6. Press record on the camera and then run:
      dvconnect --send --verbose MyMovie.dv
    Note: I run this as root for high scheduling priority. If dvconnect complains "open video1394 device: No such device or address", try loading the video1394 kernel module.

Batch Conversion Recipes

Three primary tools are available:

Transcode is a powerful tool and is initially difficult and painful to use. Mencoder seems somewhat better, but I haven't used it much. Some general tips:

Determine file type

tcprobe -i file
midentify  file

Convert Quicktime to AVI

Input may be any recognized format.

transcode -i sourcemov -y ffmpeg -F wmv2 -o dest.avi

or with mencoder:

mencoder -o dest.avi -ovc lavc -oac lavc
    -lavcopts acodec=mp3:vcodec=msmpeg4vs:vbitrate=6000
    -srate 48000 source.mov

Demux Quicktime to AC3 and MPEG 2 (VOB) for DVD

Input may be any recognized format.

ffmpeg -i source.mov -target ntsc-dvd -vn -f ac3 dest.ac3

ffmpeg -i source.mov -target ntsc-dvd -an -f mpeg2video dest.m2v

Convert AVI to Quicktime/MPEG-4 and strip audio

MPEG-4 is more compact. Note that input may be any recognized format.

transcode -i source.avi -y mov,null -o dest.mov -F ffmpeg_mpg4

Extract audio from AVI to WAV

Note that input may be any recognized format. Output is probably not the correct sample rate, or correct multiple of blocks, for audio CD.

transcode -i source.avi -y null,wav -o dest.wav

Or to extract sound from all AVI files and store in a subdirectory named audio:

for F in *.avi; do transcode -i $F -y null,wav -o audio/${F%.*}.wav; done

Convert AC3 48 KHz audio to WAV 44.1 KHz

ffmpeg suports a wide variety of codecs, and the command line options are minimal:

ffmpeg -i source.ac3 -ar 44100 dest.wav

Extract portion of VOB file to MPEG and strip audio

The VOB of interest with Xine by mounting the media and selecting the file by name. Note segment start and end offsets in Xine (press space to pause and display time offset). This example is copying from 8 minutes and 16 seconds to 14 minutes and 7 seconds (multiple time spans may be concatenated in the same option). --export_prof dvd retains the same image dimensions as the original (if it came from DVD). Transcode can copy from unencrypted VOB files.

transcode -i source.vob -c 0:08:16-0:14:07 --export_prof dvd -y ffmpeg,null -o dest

(.m2v suffix added automatically)

Note: I encountered at least one VOB where xine reported a time code which slower than real time by 50%, and transcode's clock was correct. Extract a small sample close to the beginning of the source to verify the reported time.

Downsampling video by dropping frames

The fps filter works by dropping frames. Rather course, as it only allows integral divisors, though it works well that time-lapse look. This speeds up the video by x10.

transcode -i source.mov -y mov -F ffmpeg_mpg4 -o dest.mov -J fps=299.7:29.97

Reverse the fps ratio to slow down the motion. The right-hand number should be your target frame rate.

Summarize movie times

The movie_times script reports the duration of all movie files given to it on the command line, and then adds the times together for a total.

TV Tuner/Video Capture Cards

Copyright © 2004-2006 Craig Lawson
Index no-thank-you-spam-i-am@i-hate-spam.spam.spam Document made with Nvu