Log in

View Full Version : avi->mp4 transcoding frustration


vlogger
1st December 2005, 22:58
Hi All,

I am building a video app and was originally transcoding files to xvid in an avi container. After running into some avi limitations -- namely no progressive download -- I've decided to move to .mp4. It seems to be a better standard as it's open and supported on a variety of OS's and hardware devices.

Previously, I could transcode a file in one shot with mencoder. Even though mencoder seems to support mp4 muxing, I can't generate a playable file. As for codecs, I'm currently looking at x264 and xvid as possibilities. (I have a concern about the cpu requirements about the former and Mac compatability with the latter.) I have read these forums and other sites extensively for the past week and am still have concerns with transcoding.

Here is an example (input.avi is xvid encoded):
mencoder -oac faac -ovc copy input.avi -o output.avi
mplayer output.avi -dumpaudio -dumpfile output.aac
ffmpeg -i output.avi -an -f m4v -vcodec copy output.m4v
mp4creator -hint -r 14 -c output.m4v final.mp4
mp4creator -hint -interleave -c output.aac final.mp4

This generated a playable file, but a/v sync was hosed. Also, I was wondering if there is a way to avoid specifying the rate in "mp4creator.exe -hint -r 14 -c wank.m4v mytest.mp4" as I would like to automate this process and not worry the user with details like that. This is one of the reasons I tried to stay clear of using avisynth as I don't know if it's possible to do a generic script that covers 99% of situations with it. So put simply, here are the questions:

1) Can this process be further simplified with existing tools?
2) Can this process be genericized so users don't have to know things like framerate? (Also requiring a framerate would make it particularly difficult to transcode wmv files as theirs are often variable.)
3) Are there any steps that will improve the odds of maintaining audio/video sync?
4) Any thoughts as to the trade-off between encoding/decoding speed and "out of the box" cross-platform compatability with respect to x264 and xvid?

Thanks for the help,
David

bond
2nd December 2005, 15:59
whats the sense of using mencoder to encode with faac to avi? why not simply use the faac tool to encode to .mp4 (or .aac) right away without mencoder/mplayer?

about desync, are you sure that 14fps it correct?

vlogger
2nd December 2005, 19:27
Thanks for the reply. I've been making some progress using mp4box, which I quite like.

whats the sense of using mencoder to encode with faac to avi? why not simply use the faac tool to encode to .mp4 (or .aac) right away without mencoder/mplayer?

I am transcoding, not ripping. It seems like the bulk of the avi files use divx video and mp3 audio. I tried dumping the audio (using mplayer -dumpaudio) and then using faac on the resulting file, but faac doesn't seem to handle mp3 input. In fact, I had little success converting anything as wav files got munged.

In addition, it seems that if one sets up an avi with a valid video stream and aac, then it's really straightforward to convert that to an mp4 with mp4box. Am I misunderstanding something? It seems that once I can get the aac audio working, I'll be pretty close to creating spec compliant mp4 files.

about desync, are you sure that 14fps it correct?

Yep. I assumed that the avi file was NTSC and saw that playback was weird after I converted it to an mp4. I looked at the avi and saw that the actual fps==14, reconverted and voila, it was fine. mp4box seems to use the pre-existing framerate, which is nice.

bond
2nd December 2005, 19:35
i doubt mp4box can handle aac-in-avi, its not really widely used

vlogger
6th December 2005, 05:12
i doubt mp4box can handle aac-in-avi, its not really widely used

I was afraid of this.... It seems like mp4box does a fine job with avi files that have mp3 audio. The files are all playable on both my Windows PC (with Haali and ffdshow) and on my Linux box. Unfortunately, I don't have a Mac so I can't test it there. I know that the iPod Video requires AAC Audio and MPEG-4 (ASP or AVC) Video, but does anyone know if regular Quicktime on the Mac has the same requirements? Or more importantly, does anyone know if the Mac handles MP4 files with MP3 audio tracks?

In addition to using mp4box for one step repackaging (which should work provided that the video and audio codecs are acceptable, right?) I also had some success with ffmpeg. However, there was some weirdness.

Input file: avi (divx 5 & mp3)
ffmpeg.exe -i input.avi -vcodec copy -acodec aac -ab 128 output.mp4
On my Windows box (w/ffmpeg precompiled binary), this resulted in a file that was all messed up. The original framerate was 29.97, but ffmpeg indicates that it's 30000 fps and indicates that it's going to use that value in the output stream. On my Linux machine, the same command line resulted in a playable file. I have Gentoo so ffmpeg was built from sources. I suspect that my Windows build is a bit weird. If someone thinks this is an OS thing, please let me know.

Thanks,
David

bond
6th December 2005, 12:14
Or more importantly, does anyone know if the Mac handles MP4 files with MP3 audio tracks?on windows qt doesnt handle it, so i assume on mac it also doesnt do it

In addition to using mp4box for one step repackaging (which should work provided that the video and audio codecs are acceptable, right?) I also had some success with ffmpeg. However, there was some weirdnessaac-in-avi is a hack introduced by alexnoe and i doubt any other tool lets you create such files, so dont waste too much time with wanting to edit this file with other tools

simply grab avimuxgui (the tool from alexnoe) and demux the aac stream from avi with it to .aac. and than you can feed it to mp4box for example for getting a correct aac-in-mp4 file

(btw ffmpeg creates not 100% correct mp4 files)

vlogger
7th December 2005, 00:30
aac-in-avi is a hack introduced by alexnoe and i doubt any other tool lets you create such files, so dont waste too much time with wanting to edit this file with other tools

simply grab avimuxgui (the tool from alexnoe) and demux the aac stream from avi with it to .aac. and than you can feed it to mp4box for example for getting a correct aac-in-mp4 file

(btw ffmpeg creates not 100% correct mp4 files)

Bond,
I really appreciate all of your help. This has been pretty frustrating for me, but also very informative.

Here's the deal, I am writing an app that will, before uploading a file, transcode it if necessary. I would like the resultant file to be playable on a variety of platforms. For the moment, I'm going to stay away from H264 because it's too computationally expensive. So my desired output is an MP4 file with AAC audio and MPEG-4 ASP video.

I plan on doing system calls to command line apps to handle the transcoding. Furthermore, I don't want the user to have to learn/understand video; i.e. I'd like to take the file and transcode it automatically. I am guessing that most of the files that come in will be avi, typically with MPEG-4 ASP video and MP3 audio. Using command line tools, what is the most direct, reliable way to get good MP4 (AAC+MPEG4-ASP) files?

I've tried lots of approaches, but have had very inconsistent results. Most recently, I tried the following with little success.

1) Use mencoder and generate an avi file with mpeg-4 asp and aac.
2) Use mplayer to dump the audio track and video track to separate files.
3) Mux the audio and video together with mp4box.

smok3
7th December 2005, 00:45
you can check my lil guide (http://somestuff.org/blog/static.php?page=x264_4_quicktime) (there is also a bat file that makes a lil different approach/software than guide)

bond
7th December 2005, 00:56
Using command line tools, what is the most direct, reliable way to get good MP4 (AAC+MPEG4-ASP) files?mp4box will handle the typical divx/xvid avi + mp3 file fine

vlogger
7th December 2005, 08:01
you can check my lil guide (there is also a bat file that makes a lil different approach/software than guide)

Thanks, I uncovered your guide (and bookmarked it) a few days ago. I might end up adapting what you have for use with xvid/divx instead of x264. (You refer to this and give a link, but it's broken.) I wish the audio dumping and transcoding were a bit faster, but it seems pretty reliable this way.


mp4box will handle the typical divx/xvid avi + mp3 file fine

When I learned this, I thought I had hit upon the solution. The problem is that those files won't play in Quicktime. (Easy X-platform playability is critical for me.) In fact, it seems that I can't rely on an xvid/divx file to work in Quicktime without a plugin like http://www.insaneness.com/xvid.html and divx installed. This probably has to do with b-frames not being handled correctly. The only way I know to get rid of b-frames is to re-transcode. This unfortunately will make this process lossy, but it seems unavoidable. If this is the case, is the best solution to follow these steps?

1) Extract the audio and convert it to aac. (mplayer + faac)
2) Transcode the video to no-frills mpeg4. (mencoder)
3) Mux the audio and the video. (mp4box)

One more thing. Sometime mp4box requires a framerate and other times it doesn't. Do you know the reason for this?

smok3
7th December 2005, 09:09
whats broken, link?

check the notes about audio dumping, there is fast but a lil less reliable way, and a slow but working way using mplayer..., however the bat file uses vdub.exe.

vlogger
7th December 2005, 17:44
whats broken, link?

Yeah. At the bottom of your guide, you have this:
you could also use XVID for the video part, even without avisynth, for example:
mencoder_xvid_on_winxp.html (http://somestuff.org/blog/mencoder_xvid_on_winxp.html?PHPSESSID=a45f10f0b394a81d814fc03b757939d4)

check the notes about audio dumping, there is fast but a lil less reliable way, and a slow but working way using mplayer..., however the bat file uses vdub.exe.

mplayer INPUT -dumpaudio -dumpfile OUTPUT

The above always says that it's core dumping, but I get a valid and complete audio file. However, it's not viable because it dumps the audio in its current format, which is generally not going to be something that faac can handle. Unless I'm missing something, the audio would have to be transcoded to pcm and then dumped, which eliminates any of the time savings.

bond
7th December 2005, 19:24
When I learned this, I thought I had hit upon the solution. The problem is that those files won't play in Quicktime. (Easy X-platform playability is critical for me.) In fact, it seems that I can't rely on an xvid/divx file to work in Quicktime without a plugin like http://www.insaneness.com/xvid.html and divx installed. This probably has to do with b-frames not being handled correctly. The only way I know to get rid of b-frames is to re-transcode. This unfortunately will make this process lossy, but it seems unavoidable. If this is the case, is the best solution to follow these steps?

1) Extract the audio and convert it to aac. (mplayer + faac)
2) Transcode the video to no-frills mpeg4. (mencoder)
3) Mux the audio and the video. (mp4box)qt only handles simple profile (and avc) video, not advanced simple profile. which means it doesnt support b-frames, qpel, gmc, cqm...

One more thing. Sometime mp4box requires a framerate and other times it doesn't. Do you know the reason for this? mp4box needs a framerate when muxing from raw video streams

smok3
7th December 2005, 22:18
ok, links are fixed now hopefully, http://somestuff.org/x264_qt/h264_in_quicktime7.html
(yes my guide is dvavi -> mp4 centric, you will have to figure out some additional details)

hitbit
8th December 2005, 12:36
mplayer INPUT -dumpaudio -dumpfile OUTPUT

The above always says that it's core dumping, but I get a valid and complete audio file. However, it's not viable because it dumps the audio in its current format, which is generally not going to be something that faac can handle. Unless I'm missing something, the audio would have to be transcoded to pcm and then dumped, which eliminates any of the time savings.

This line convert the audio of INPUT to aac, as long as mencoder is built with faac enabled:

mencoder.exe INPUT -ovc copy -oac faac -faacopts br=128 -of rawaudio -o audio.aac

multicone
8th December 2005, 14:34
You could try muxing the AVI to MKV first, using 'native mode' switch in mkvmerge, and then try to create a MP4 from the MKV ? Maybe on DirectShow, using 3ivX MP4 muxer filter ? Note i have no time to test for myself, so you are on your own.