A GUIDE ON HOW TO EDIT MPEG4 FILES IN (DIVX).AVI, .mkv and .mp4 Containers -- JULY 19, 2009


Increasingly, MPEG4 material rather than MPEG2, is being used and found on the Internet in divx.avi, .mkv and .mp4 files.

While many good MPEG2 editors exist, and some like Womble's MPEG Video Wizard will edit and output with minimal transcoding, there is, to date, no equivalent MPEG4 editor that I am aware of.

Based on my own research, here is a process to fully edit a divx.avi, .mkv or .mp4 file containing MPEG4 format video and any format audio, along with any associated subtitles, and with NO transcoding! The instructions may seem complex because I have detailed them precisely, but in actuality it is very simple and the steps are equivalent to any editing process. It also offers advanced capabilities to increase precision and fully synchronize subtitles. The only issue is that the edits occur at video I-frames and so will not be frame accurate and will likely cut or retain some excess material. Picking wide edit cut points can address this problem.

The Process

First we determine the cut points for our edits, then split the file into segments at those points, then merge the segments we wish to keep. The process can then be repeated for greater precision or to correct errors. If there is a subtitle file we then remove the subtitles that were part of the removed segments, and adjust the timing of the retained subtitles to be close to the edited points. Finally we adjust this subtitle file to be fully syncronized with the audio of the edited file.

The process uses 3 free tools, Subtitle Workshop 2.51 from http://www.urusoft.net/download.php?lang=1&id=sw , MkvMerge-GUI (mmg.exe) - part of the mkvtoolnix package from http://www.bunkus.org/videotools/mkvtoolnix/downloads.html, and Media Player Classic from http://sourceforge.net/projects/guliverkli/. A fourth tool YAMB Beta from http://yamb.unite-video.com/download.html is required if the final file is to be .mp4 . Download and install these programs from the Internet. It also uses a spreadsheet program to manage the process. Excel is described but any spreadsheet program or even paper can be used.


The file to be edited must be in .mkv format and the subtitles, if any, synchronized with the audio. If the file is in divx.avi or .mp4 format it may be converted quickly to .mkv using Mkvmerge. Simply add the .mp4 using the input tab, change the output extension to .mkv and click "Start Muxing". Then using Subtitle Workshop verify or adjust the subtitles to be in sync. The edit may then be performed and when complete the edited .mkv file may be converted back to .mp4 with YAMB Beta.

A second consideration is the audio format in the edited file. The original Divx.avi files may contain .mp3 audio and .mp4 files may contain AAC audio, but if AC3 is required for decoding by external amplifiers, the edited .mkv file containing the .mp3 or AAC can be easily and quickly converted to AC3 using XviD4PSP from http://www.winnydows.com/download.php?view.39 and output as either .avi or .mkv with the video copied as is. Since XviD4PSP does not support AC3 in .mp4 files the converted .mkv file with AC3 may then be converted back to .mp4 with YAMB Beta.

The following editing process will be easier if the file to be edited, and its subtitle file if any, is moved to its own subdirectory.

Find the Cut Points

We will use Subtitle Workshop to obtain our cut points, working in Frames rather than Times, and then save them in a subtitle file.

First start Subtitle Workshop, Click on File - New Subtitle, then on Mode to Switch from Time to Frames, and save your subtitle file in Pheonix format as Yourname.pjs in the movie directory. (This is necessary because we need the subtitle file data to be in frames, which .pjs provides, rather than time.)

Second, click on Movie - Open and select the .mkv file you wish to edit. The framerate box will automatically change to match the movie framerate. Make a note of it because you will use it below.

If the movie has a real subtitle file, it is possible, by using the subtitle display support in ffdshow, for them to be displayed in the Subtitle workshop preview window as you play the movie and determine the cutpoints. To do this you must have ffdshow video decoder installed with its default high priority (merit) and have its subtitle feature enabled. If this decoder is active it will display an icon in the system tray that you may click to obtain the decoder menu and enable subtitle display. It is best if cut points are selected between subtitles.

Now play the movie, pausing it at each of the desired cut points. These will be the start and end points of the segments you want to keep, in sequence from the start to the end of the movie. Because the edits will actually be at the "I" frame following these cut points, try to pause at points a few seconds earlier than the desired point in order to reduce the clipping of desired material.

The keys that will assist with this are:

Ctrl+Space - to play and pause
Ctrl+Tab - to Toggle Full Speed / 10% Playback Speed
Ctrl+Left Arrow - Back up 5 seconds
Ctrl+Right Arrow - Jump Ahead 5 seconds
Alt+Left Arrow - Back up .5 seconds
Alt+Right Arrow - Jump Ahead .5 seconds

Record the Cut Points

As you determine each cut point, save it by creating a new frame number marker in the subtitle file with the sequence:

Alt+Z to Set Cut Start Time (This has no visibe effect), then Alt+X to Insert the New Marker with equal Start and Hide Frame Numbers.

You then MUST type some text for this marker.. you can describe the clip or just put a z for a keeper segment and an x in a segment to be cut. If you do not enter any text, the marker will not be saved in the saved subtitle file and the process will break !!!.

At any point you can double-click on any marker and play the video from that point, or you can delete it and backup or go forward and reset the Marker Times as above. The only important point is that the markers must be ascending sequence, and not have hide frame numbers less than the start frame number or overlapping the next marker start, otherwise they will again not be saved.

Once you have created all the cut point markers, review them as required and then save the subtitle (YourName.pjs) file and exit Subtitle Workshop.

Build Your Cut String

Now Open Excel and then open the YourName.pjs subtitle file as "fixed width" and press finish. This will load the frame data into columns. Other spreadsheet programs may need to have the data entered manually. Since we will be using this data, I will refer to its values below by Column Letter.

You now begin with a file loaded with all your cut points, start frame #(A), end frame #(B), and a description (C). (A) and (B) should be equal and (C) should indicate if the segment is to be cut or kept.

Using Excel, create a new column (D) = Start Frame/Movie Frame Rate. These are the cut points in seconds that you will use to make the cuts.

You can also use the data in (A) to calculate a column (E) with the length of each segment in Frames. You will need to know the length of the cut segments if you have and plan to adjust a subtitle file to match the edited video as described below.

With the frame length data you may also calculate a column (F) with the target starting frame number for each segment kept in the final edited video, but since the actual cuts will be at the next I frame, these will only be approximate. Again this frame number data will be useful in the final adjustment of subtitle timing.

Finally, in any cell (X), create a string of all the cut point seconds from (D) in the form nnnns,nnnns,nnnns eg: 4s,25s,892s,100s and copy it to the clipboard.

Perform and Adjust the Cuts

To cut the Movie into the defined segments, run MkvMerge as follows:

On the Input Tab, Add the Movie, and set the Output Name that will be used to create a set of numbered clips. Since the movie file name is pre-entered in this box, the easiest step is to add -cut to the name. ie: originalname-cut.mkv

On the Global Tab, click Enable Splitting and After Timecodes. Then paste the cut point string (from (X)) into the box, and click "start muxing".

The movie will now be broken into multiple clips, numbered in sequence in the output directory. On completion, just minimize MkvMerge to retain the existing info, in case you need to adjust the clips and run it again.

At this point you can play these clips to check the accuracy of the cuts. Since these cuts occur at the next I frame after the cut frame, it is possible that the start of the desired clip is missing a few seconds, or that the end of the clip contains excess material.

Fixing this is more more complex, because properly you need to determine how far back the previous I frame is from the requested cutpoint in order to adjust the cut point back to be before the previous I frame. There is no precise way to do this. The approximate way is to determine the length of the previous I frame segment and move the cut back that amount.

This can be determined by opening the previous or current clip using Media Player Classic, and after pressing pause, click on the time line near the end of the video. Then press Shift+Right Arrow.

If the time or cursor does not move, you are within the previous I frame segment and you need to press Shift+Left Arrow to back up to the previous I frame. If the time or cursor does move you are not yet at the last I frame so continue to press Shift+Right arrow until it does not move. Now note the two times under the time line. The difference is the length of the previous I frame segment in seconds that needs to be subtracted from the current cut point.

Alternatively, if a clip starts or ends too early, you will need to move the cut points forward. Determine the adjustment by pressing Shift+right arrow at the start of the current or next clip, which will moves it to the next I frame. The elapsed seconds are the length of the next I frame segment in seconds that needs to be added to the current cut point.

Determine these adjustments for each clip, and enter the adjustment as a minus or plus seconds in a new column (G) on the Excel sheet, against the clip if adjusting the start point, or the next clip if adjusting the end point. Then calculate the new cutpoints in column (H)=(D)+(G) and modify the cut string (X), to contain these new values from (H). Finally copy the string to MKVMerge and redo the split.

Repeat this process, accumulating further adjustments in column (G), and adjusting (H) and (X) until it the edited clips are correct.

Create the Edited File

Once you are happy with the clips, run MkvMerge again but this time you will just join the clips (every second one) that you want to keep.

On the Input Tab, after Clicking Remove All, Add the first clip you want to keep. Now click append and using Ctrl+click select all the clips (every second one) that you want to keep, and click Open. Finally set the Output name for your edited file ( add -cut ) and click "Start Muxing".

On completion, check your edited video for errors in case you need to repeat any of the above.

Edit Subtitle Timings

If you have a subtitle file for the original video it may now be edited remove subtitles in the cutout segments and adjust the timings to match the edited video.

As described above, from the Excel Spread sheet you can see the original cut point frame numbers, calculate the number of frames that were cutout in each segment, and the approximate new starting frame number of each segment.

Open the Subtitle file with Subtitle Workshop, save it under a new name ( add -cut to match the edited video ) so the original one is retained, switch to frames, and if required change the framerate to that of the movie as you noted above. NOTE: That no movie is loaded here. Also NOTE: That these subtitle file adjustments MUST be done backwards beginning with the last cut at the end of the subtitle file back to the start.

Using the original frame numbers from the Excel sheet (A), select the first subtitle with a frame number that is within in the LAST cutout segment. Delete this and all following subtitles that have starting frame numbers within the range of the cutout segment ((A) to (A)).

Now adjust the timing of the remaining following subtitles to account for the cutout segment by selecting the next subtitle (the first in the following kept segment), consider adding #### to the start of it (see below), then select all subtitles from there to the end of the file (Ctrl+End, Shift+Click). Finally use Ctrl+D for Set Delay and Subtract the number of frames that were cutout in the previous segment as shown on the Excel sheet (E), click "for selected subtitles" and "Apply". NOTE: This changes the frame numbers on the following subtitles, but the preceeding subtitles still have their original frame number values that match those on the Excel sheet (A).

Repeat this process for each cutout segment, working backwards to the first segment. Again it is useful to keep track of your progress with notes on the Excel spreadsheet, perhaps noting the new frame number of the first subtitle in the last retained segment in Column (I) so that if you make an error, you can use the Edit Menu undo/redo to back up to this point .

When complete save the adjusted subtitle file.

Fine Tune the Subtitle Timings

Finally, because the actual cuts were aligned with the I frames and will not be the same number of frames that we just applied to the subtitles, the subtitles may be slightly out of sync with the edited movie file. The only way to fix this is using Subtitle Manager and together with the edited movie and adjust the subtitle timing within each segment as required.

To do this it would be useful to know the subtitle numbers of the subtitles at the start of each kept segment, however this cannot be deduced from the Excel spreadsheet. As described above however you can calculate the approximate adjusted frame number of each new segment (F), and use this to find the closest subtitle. Another way is to have some unique text such as #### to identify the first subtitle in each segment.

With these frame numbers or markers, play back can then be initiated at subtitles at the start of each segment, and if required, the subtitle timings adjusted from that point up to to the next #### or for the rest of the subtitles by selecting them and using Ctrl+D for Set Delay to Add (if the subtitles are early) or Subtract (If the subtitles are late) the number of frames needed to put them in sync. The Edit Menu undo/redo may be useful during this process.

Final Conversions

As detailed in the Overview, the edited .mkv file may have its audio converted to AC3, and/or the .mkv converted to .avi or .mp4