Basic video editing with FFmpeg on Linux

Here’s a small guide editing your DV/HDV clips with ffmpeg under Ubuntu Linux. All cutting, processing, and exporting is done via the command line (oh, the joy!). Sure, this method doesn’t have niceties like fades, titles, effects etc, but it works for basic editing. Please note that not all AVCHD formats from all such cameras are supported.

First, you need to install the right version of FFmpeg, that supports all codecs (the default Ubuntu version doesn’t, for legal reasons). Follow the “B” solution from this tutorial to install the full version of ffmpeg on the latest versions of Ubuntu.

Now, find the DV/HDV or AVCHD video files you want to trim, or slice & cut. Watch a video file on a video player (e.g. VLC or Totem), and on a text editor (e.g. gEdit) type the minutes/seconds you want to cut. For example, you can type:
video1.m2t
– section 1: keep from 00:00:01 to 00:00:15
– section 2: keep from 00:02:37 to 00:03:54
video2.m2t
– blah-blah…
– blah-blah…

Then, load your glorious terminal, navigate to your videos’ folder, and type:
ffmpeg -ss 00:00:01 -t 00:00:15 -i "video1.m2t" -acodec copy -vcodec copy "edited-video1.m2t"
This means that ffmpeg will only save to a new file the video from the first second up to the 15th second. Adjust the numbers according to your needs. The transcoding will be completely lossless and done in mere seconds. Do the same for all your other sections/files. The original files won’t be destroyed btw, so you can re-cut them if needed.

After you have done this for all your files, you can put together the various edited files in a single file!
cat edited-video1.m2t edited-video2.m2t edited-video3.m2t > chapter1.m2t
Warning: The files must be of the exact same type! You can’t mix and mash different formats!

Now, you might want to burn a DVD! Here’s how to create a widescreen PAL DVD:
ffmpeg -i "chapter1.m2t" -aspect 16:9 -target pal-dvd "chapter1.mpg"
Use “ntsc-dvd” for NTSC videos instead of “pal-dvd” above. Also, you can have many different “chapter” files, that can act as different chapters to your DVD. You can use an app like DeVeDe to put together the various edited chapters/videos, and burn your DVD.

And here’s how to export for YouTube/Vimeo/PS3/XBoX360 in 720p HD h.264/AAC .MP4. First, let’s put all the chapters in a single video (if you have gone that chapters route):
cat chapter1.m2t chapter2.m2t chapter3.m2t > myvideo.m2t
And then, do the final export:
ffmpeg -deinterlace -i "myvideo.m2t" -threads 2 -f mp4 -vcodec libx264 -level 41 -refs 2 -loop 1 -deblockalpha 0 -deblockbeta 0 -parti4x4 1 -partp8x8 1 -partb8x8 1 -coder 1 -subq 6 -brdo 1 -me_range 21 -s 1280x720 -r 30000/1001 -b 5120k -bt 8192k -bufsize 15000k -maxrate 16000k -g 300 -acodec libfaac -ac 2 -ab 128k "myvideo.mp4"
Use “-r 25” instead of “-r 30000/1001” if you are on a PAL instead of an NTSC region.

And one last tip, for those who want the ultra-widescreen cinematic look. Add the code below to your (final) exporting code of ffmpeg, and adjust the numbers accordingly to your taste and create horizontal letterboxing:
-padtop 44 -padbottom 44 -padcolor 000000

Then, sit back, and enjoy the video on your TV. Oh, did I mention before that my TV runs on Linux?

9 Comments »

Andy McCall wrote on May 20th, 2009 at 1:58 PM PST:

Nice post, thanks.

One quick question – what do you mean your TV runs Linux? Do you mean your PC, running Linux, is connected to the TV, or do you mean the firmware of your TV is based on Linux? Or, do you mean something totally different 🙂

Thanks!


This is the admin speaking...
Eugenia wrote on May 20th, 2009 at 1:59 PM PST:

The firmware of the TV (or part of it) runs Linux.


kragil wrote on May 20th, 2009 at 7:01 PM PST:

Tom wrote on May 20th, 2009 at 7:20 PM PST:

But…but you don’t like Linux!


Jorge wrote on May 20th, 2009 at 7:55 PM PST:

Thank you Eugenia for this post! I was thinking about that the other day..how to trim & put together my HV30 M2T files in a lossless way. Nice wrap up!


sorta works wrote on May 24th, 2009 at 1:25 PM PST:

The ffmpeg seek (-ss) and duration (-t) arguments worked on a Quicktime file.

However, they don’t seem to work on an AVCHD from a Canon HF11 (.mts).

Also, I can’t get them to work on flash (.flv) files.

By the way, I am keeping these two ffmpeg arguments positioned in the command before the input file argument.


This is the admin speaking...
Eugenia wrote on May 24th, 2009 at 1:28 PM PST:

I only tried them with the latest ffmpeg, and with HDV .m2t files. HV20/30 HDV users comprise the primary readership of my blog.

FFmpeg and mencoder suck with AVCHD files, they have many bugs. Apparently, there have been a slew of patches for AVCHD, but the maintainers are very slow integrating them to their stack, hence the eternal incompatibilities with AVCHD.

More over, you can’t do this kind of processing shown in the tutorial if ffmpeg can’t export in the same format. For example, you can’t cut an AVCHD file and re-exporting it as AVCHD, because ffmpeg doesn’t know how to export in AVCHD. You might want to cut it, and then export it as a different file format, e.g. the Avid DNxHD, or HDV .m2t.


sorta works wrote on May 24th, 2009 at 1:45 PM PST:

Thank you for the quick reply.

You’re probably right: currently, ffmpeg cannot properly export AVCHD (.mts) nor flash (.flv) files.

By the way, I am using an SVN version of ffmpeg built last Thursday. Below is the result of my “ffmpeg -version”, for anyone who is interested.

Thanks.

==================

$ ffmpeg -version
FFmpeg version SVN-r18890, Copyright (c) 2000-2009 Fabrice Bellard, et al.
configuration: –prefix=/usr –extra-cflags=’-Wall -g ‘ –cc=’ccache cc’ –libdir=${prefix}/lib –shlibdir=${prefix}/lib –bindir=${prefix}/bin –incdir=${prefix}/include/ffmpeg –enable-shared –enable-libmp3lame –enable-gpl –enable-libfaad –mandir=${prefix}/share/man –enable-libvorbis –enable-pthreads –enable-libfaac –enable-libxvid –enable-postproc –enable-libamr-nb –enable-libamr-wb –enable-x11grab –enable-libgsm –enable-libx264 –enable-libtheora –enable-libdc1394 –enable-libspeex –enable-nonfree –disable-stripping –enable-avfilter –enable-libdirac –disable-decoder=libdirac –enable-libschroedinger –disable-encoder=libschroedinger –enable-avfilter-lavf –enable-libopenjpeg –disable-altivec –disable-armv5te –disable-armv6 –disable-vis
libavutil 50. 3. 0 / 50. 3. 0
libavcodec 52.29. 0 / 52.29. 0
libavformat 52.33. 0 / 52.33. 0
libavdevice 52. 2. 0 / 52. 2. 0
libavfilter 0. 5. 0 / 0. 5. 0
libswscale 0. 7. 1 / 0. 7. 1
libpostproc 51. 2. 0 / 51. 2. 0
built on May 21 2009 08:51:17, gcc: 4.3.3
FFmpeg SVN-r18890
libavutil 50. 3. 0 / 50. 3. 0
libavcodec 52.29. 0 / 52.29. 0
libavformat 52.33. 0 / 52.33. 0
libavdevice 52. 2. 0 / 52. 2. 0
libavfilter 0. 5. 0 / 0. 5. 0
libswscale 0. 7. 1 / 0. 7. 1
libpostproc 51. 2. 0 / 51. 2. 0


This is the admin speaking...
Eugenia wrote on May 24th, 2009 at 1:59 PM PST:

I just tried to cut an AVCHD HF11 .mts file I had around, and it did cut. I saved it as Avid DNxHD for video (145Mb bitrate), and pcm_s16le for audio. HOWEVER, as I said above, FFmpeg is so bloody buggy with AVCHD, that it thinks that 60i is 60 progressive frames, and so you end up with DOUBLE the time. For example, a 3 second AVCHD file will get saved as a 6 second one! And if you add the interlaced flags for DNxHD, you will end up with 12 seconds! The bug is in ffmpeg. You just need to go to their dev mailing list, and start shouting at them for not having fixed that bug for months now. They have no excuse in my opinion, given the popularity of the format. Please note that not all AVCHD files behave the same on ffmpeg. Some files from other manufacturers exhibit different behavior.


Comments are closed as this blog post is now archived.

Lines, paragraphs break automatically. HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

The URI to TrackBack this blog entry is this. And here is the RSS 2.0 for comments on this post.