Dirk Bertels

The greatest malfunction of spirit
is to believe things (Louis Pasteur)

Editing wtv files with FFmpeg

Last updated 08 May 2011


Index

Introduction
Using ffmpeg
Creating clips
Joining clips
More FFmpeg commands
Links and References
Comments

Introduction

wtv files are those files that windows Media Center creates when recording TV programs. Media Center comes with windows 7 and I use it in combination with a Tiny Twin high definition TV tuner, which is a USB device that actually contains 2 tuners so you can watch (and record) one channel while recording another one. So far it has worked quite well.

On Windows 7 you can burn wtv files to disc using the windows DVDMaker (which also comes with windows 7), however it gets more complicated when you want to edit a movie (like removing ads or cutting clips) or when the movie is too long to burn to one (single sided) disc.

There's the option of editing the movie using Live Movie Maker but the quality never seems to be satisfactory.

This article describes a simple work-around, using the command line, that converts a wtv movie into a standard mpeg format and does some basic editing (cutting and splicing). All this can be done using just 2 commands. I also added some more commands and information for reference.

back to index

Using ffmpeg

Elsewhere on this site I wrote an article, Video editing using free software, featuring the non-linear editor Avisynth. According to Wikipedia, non-linear editing is an editing system that can perform random access on the source material. The term linear hints to the old ways of using tape and film. This time we will use a command line tool that does the actual encoding and conversion on the fly: ffmpeg. FFmpeg is used by the major frameworks Directshow, Quicktime, Gstreamer, OpenMax and by many open source projects such as VLC, MPlayer, Blender and Google Chrome.

Typically a Linux tool, ffmpeg is also available for windows. The windows version can be downloaded from hawkeye.arrozcru.org. The zip file will be named something like ffmpeg-git-c967860-win64-static for a 64 bit machine. When extracted, you will see a few executables (including ffmpeg.exe) and a few folders, including the all important doc folder. In order to create a simple work environment, I did the following:

  1. Create a folder FFmpeg in your Program Files folder and copy all the ffmpeg files and folders into it.
  2. Add the path to FFMpeg in Control Panel → System → Advanced System Settings → Advanced → Environment Variables → System Variables → Path → Edit. At the end of the line, add a semicolon followed by the path to your FFmpeg folder, which should be something like ;C:\Program Files\FFmpeg
  3. Create a work folder in a suitable location
  4. Create a batch file to open the command line in this work folder. This batch file is just a text file that contains the word cmd. Rename the file with a bat extension (like work.bat). When you double click on it the command line will open in the work area's directory. This relieves you of the arduous task of having to add the paths to the files each time you run a command.
  5. Copy the wtv file you want to edit in the work folder also.

We are now ready to do some basic editing.

back to index

Creating clips

Assuming we want to cut a segment from the wtv file, starting at position 00:35:00 and lasting 28 seconds, and convert it to a suitable format for PAL DVD, then the following code should do:

ffmpeg.exe -y -i sample.wtv -vcodec copy -acodec copy -ss 00:35:00 -t 28 -target pal-dvd sample.mpg

This will create the sample.mpg file which we will now use to edit. The parameters are:

  • -y   overwrite output files
  • -i sample.twv   input file name
  • -vcodec copy   copy the video codec as is
  • -acodec copy   copy the audio coded as is
  • -ss   seek to given time position in seconds or hh:mm:ss[.xxx]
  • -t   restrict the transcoded/captured video sequence to the duration specified in seconds or hh:mm:ss[.xxx]
  • -target pal-dvd   force dvd format ( -f dvd also works)

American and Canadian viewers should replace pal with ntsc.

back to index

Joining the clips

To join the clips, you don't even need ffmpeg, the windows command copy (cat in Linux), will do the trick:

copy /B clip1.mpg+clip2.mpg+clip3.mpg  final.mp4

The /B indicates that we are using binary files here. The '+' operator does the concatenation.

Using just these 2 commands enable you to edit videos in a very rudimentary fasion - this is sufficient for operations such as removing ads and cutting the movie in sections if it doesn't fit on a single sided DVD. The mpg (mp4) format also ensures that the movie can be played on a standard media player and is ready to burn to DVD..

back to index

More FFmpeg commands

Get version

To get the current version of FFmpeg

ffmpeg -version

Get available formats

ffmpeg -formats

Subtitling

PAL systems use teletext captioning while NTSC systems use closed captions. Teletext files are kept in srt files. While it is possible to extract closed captions from a wtv file (see DVR-MS: Adventures in Closed Captioning, it is much harder to do so for Media Center's teletext. I only found one solution which involves quite a few programs: TT subripper. method. It uses Directshow's Graphedit to capture the teletext.

The following command to extract subtitles to a text file will not work with pal systems:

ffmpeg -i file.mov -an -vn -sbsf mov2textsub -scodec copy -f rawvideo sub.txt

INFO:
-an disable audio recording
-vn disable video recording
-sbsf mov2textsub     Bitstream filters

Get Video information

This command will tell you that you need to specify an output file, but will give you all the information on the video file as well.

ffmpeg -i <your video or audio file> 

My wtv example gave:

Input #0, wtv, from 'sample.wtv':
Metadata: ... about 68 lines of metadata ...
Duration: 00:17:02.04, start: 5698.122076, bitrate: 6100 kb/s
Stream #0.0[0x74](eng): Subtitle: [0][0][0][0] / 0x0000
Stream #0.1[0x75](eng): Audio: mp2, 48000 Hz, stereo, s16, 256 kb/s
Stream #0.2[0x76]: Video: mpeg2video (Main), yuv420p, 720x576 [PAR 64:45 DAR 16:9], 
                   10000 kb/s, 25.20 fps, 25 tbr, 10000k tbn, 50 tbc
Stream #0.3[0x0]: Attachment: mjpeg

After conversion to dvd format,

Input #0, mpeg, from 'final.mp4':
Duration: 00:00:38.96, start: 1.000000, bitrate: 8284 kb/s
Stream #0.0[0x1c0]: Audio: mp2, 48000 Hz, stereo, s16, 256 kb/s
Stream #0.1[0x1e0]: Video: mpeg2video (Main), yuv420p, 720x576 [PAR 64:45 DAR 16:9], 
                    10000 kb/s, 25.21 fps, 25 tbr, 90k tbn, 50 tbc

INFO:
tbn = time base in AVStream that has come from the container
tbc = time base in AVCodecContext for the codec used for a particular stream
tbr = guessed from the video stream and is the value users want to see when they 
      look for the video frame rate.

A timebase is like a clock, and indicates ticks per second. Generally, higher values are better. Note that the video stream is identical except for the bitrate and the tbn.


Convert for YouTube

The general specs for a youtube video are:

format: flv
length < 11 min
bitrate: < 350 kbits/sec
aspect: 1.33:1 (320 x 240)

And the FFmpg command:

ffmpeg -i YourFile.mov -t 6.8 -ar 22050 -acodec libmp3lame \
-ab 32k -r 25 -s 320x240 -vcodec flv -qscale 9.5 out.flv

INFO:
-ar     audio rate in cycles per second
-ab     audio bit rate in bits per second
-r      framerate in frames per second
-s      dimensions in pixels
-qscale quantization scale (quality scale - lower numbers are higher quality)

Use automatic settings for DVD formnat

ffmpeg -i YourFile.mov -t 6.8 -target pal-dvd out.mpg

Letterbox format is 16:9 (e.g.720:406) - values must be even Use cropping to achieve format and pad output with color black.

ffmpeg -i YourFile.mov -t 6.8 -croptop 36 -cropbottom 36 -padtop 36 \
-padbottom 36 -padcolor 000000 -target pal-dvd out.mpg

Screen capture

This functionality requires the ffmpeg to be installed with the x11grab flag, which doesn't seem the case in the windows installation. So the following code should only work on Linux:

ffmpeg -f x11grab -s 320x240 -r 12 -i :0.0 -r 29.97 -qscale 2 out.mpg

Using a still image

ffmpeg -loop_input -i YourPic.png -r 29.97 -t 10 -qscale 2 out.mpg

Doing a slow motion

ffmpeg -i YourFile.mov -t 6.8 -an -f yuv4mpegpipe - | yuvfps -s 15:1 -r 15:1 | ffmpeg \
-f yuv4mpegpipe -i - -s 320x240 -f mpeg2video -r 29.97 -an -qscale 2 slow.mpg

INFO:
-yuv4mpegpipe   convert to raw format that can be piped to standard output
-yuvfps         changes the framerate without changing the timing
                use -s to convince it to change the timing 
                -s   input video framerate
                -r   output video framerate
                original framerate is 29.97, so 15 cuts it in half

Join clips

Convert to mpeg first. Use cat command in linux.

copy /B science_01.mp4+science_02.mp4  final.mp4

Add a soundtrack

ffmpeg -i YourFile.mpg -i audio1.mp2 final.mpg

Burn to DVD

For Linux, use tovid to burn the dvd. In windows, ImgBurn seems to work well, as does FAVC for more comprehensive burning (I covered FAVC in Video editing using free software).

back to index

Alternatives

VideoReDo is a very good wtv editor. Unfortunately it doesn't seem to preserve the teletext, even when you save as wtv, the teletext has disappeared. It also is commercial and costs about AUD 100

There's a GUI version of FFmpeg, called winff, but this mainly focusses on conversion, not editing.

back to index

Links and references

FFmpeg FAQ
Converting wtv to mpg in windows 7
Useful FFmpeg syntax examples

back to index


Comments