Cut/Split mp4

607 views
Skip to first unread message

Christopher Burkey

unread,
Aug 24, 2011, 11:28:28 AM8/24/11
to mp4parser-discussion
Hello,

Thanks for making this available. Am looking to integrate this library
into our open source product called EnterMedia.

Can you provide an example of how to output an MP4 file that is
shorter than the original? We would like to stream out video to a
flash video player using all Java code if possible.

Was looking for something like:

new IsoOutputStream(bos).seekToTime(23.2)

Or even more high level:

Movie movie = new MovieCreator().build(new IsoBufferWrapperImpl(new
File(pathname)));

long startmilliseconds = 2420;
long endmilliseconds = movie.getMovieMetaData().getLength();
movie.cropTimeline(startmilliseconds, endmilliseconds)

Thanks again!

regards,
Christopher

Sebastian Annies

unread,
Aug 24, 2011, 12:27:35 PM8/24/11
to mp4parser-...@googlegroups.com
Hi Christopher,

I quickly implemented what you are looking for. I added a class named CroppedTrack that wraps a normal Track and just shows the samples between fromSample and toSample to the outside.
Have a look at this example:
http://code.google.com/p/mp4parser/source/browse/trunk/examples/src/main/java/com/googlecode/mp4parser/Shorten.java

be aware: I reference a file on my disk.  I could not use the count-video.mp4 I normally reference in the examples since it only has 1 sync sample. You should always cut a video at its sync samples! video

Best Regards,
Sebastian

Christopher Burkey

unread,
Aug 24, 2011, 2:40:35 PM8/24/11
to mp4parser-...@googlegroups.com
Wow, thank for the pointers here.

I was able to test it and it runs very quickly and does seem to cut down the size of the output.

Unfortunately the output is a bit corrupted.

I also tried to add audio track and it does seem to crop but still have a slightly corrupt output.

ffmpeg is reporting these kind of errors:

[h264 @ 0xa0e2970]Missing reference picture
[h264 @ 0xa0e2970]decode_slice_header error
[h264 @ 0xa0e2970]Missing reference picture
[h264 @ 0xa0e2970]decode_slice_header error
[h264 @ 0xa0e2970]Missing reference picture
[h264 @ 0xa0e2970]decode_slice_header error
[h264 @ 0xa0e2970]Missing reference picture
[h264 @ 0xa0e2970]decode_slice_header error
[h264 @ 0xa0e2970]Missing reference picture
[h264 @ 0xa0e2970]decode_slice_header error
 
My hope was to end up with a valid output file with audio included.

Sebastian Annies

unread,
Aug 25, 2011, 4:51:48 AM8/25/11
to mp4parser-...@googlegroups.com
Hi Christopher,

I put some more work into the Shorten example. It can now handle movies with audio+video and takes seconds as from-to-parameters. The issue with missing reference picture should be gone for most cases. BUT: there could be the case where the video contains B-frames with forward references at the end of the video where that referenced samples have been cut off. This should not be the case very often. One could prevent that by correcting the end to the sample before an I-Frame. As far as I understood H264 (and I suppose we are talking about H264 videos) there should not exist any forward reference across an I-frame (reference | I-frame | referencee). But whatever - even though a referenced frame is missing at the end there should only be a minor glitch.

Best Regards,
Sebastian

Christopher Burkey

unread,
Aug 30, 2011, 2:18:23 PM8/30/11
to mp4parser-...@googlegroups.com
Sebastian,

Thanks so much for those changes and example code. It seems to be
working. mp4parser is very cool and useful, congrats!


On Thu, Aug 25, 2011 at 4:51 AM, Sebastian Annies

Reply all
Reply to author
Forward
0 new messages