avi h264 to mp4 (without transcoding)
HI, I have many avi's that are mostly encoded from pal dvd's. They were encoded using ffmpeg, although I don't recall the precise command I used as it was some time ago. I don't think they were de-interlaced during encoding. The video is encoded as h264 with the original ac3 soundtrack. I have been playing them very effectively through xbmc on my HDTV for some time. I would now like to make them iOS compatible by putting them in mp4 containers and adding an aac audio track as the first audio track. I'm trying to avoid having to re-encode so that it can be done quickly and without loss of video quality. I have tried to achieve this with both ffmpeg and also mp4box, but have hit road blocks with both for different reasons. So, I thought now I'd give mencoder a go. So far I have the following command: mencoder video.avi -of lavf -lavfopts format=mp4 -ovc copy -oac faac -faacopts br=160:mpeg=4:object=2:raw -channels 2 -o video.m4v The first problem that I need to solve is that the resulting video plays in various media players including vlc and mplayer, however when played in itunes there is no video and when played on a ipad the player simply crashes with no error message. The second problem is how do add the original sound track as a second sound track? Thanks for any help Phil
On 2011.07.22 02:01 AM, Phil Sturges wrote:
mencoder video.avi -of lavf -lavfopts format=mp4 -ovc copy -oac faac -faacopts br=160:mpeg=4:object=2:raw -channels 2 -o video.m4v
The first problem that I need to solve is that the resulting video plays in various media players including vlc and mplayer, however when played in itunes there is no video and when played on a ipad the player simply crashes with no error message. .m4v is the extension for raw MPEG-4 Part 2 video streams. VLC and MPlayer see past the extension, but proprietary players (especially hardware players) don't always do that. Also, the lavf muxer (at least in MEncoder) is broken and most players will have a hard time with the file if the video has B-frames. MP4Box is a wonderful tool for creating good MP4 containers.
Andrew Berg <bahamutzero8825 <at> gmail.com> writes:
Also, the lavf muxer (at least in MEncoder) is broken and most players will have a hard time with the file if the video has B-frames. MP4Box is a wonderful tool for creating good MP4 containers.
Andrew, if you recall, you had some input when I asked about this issue on the ffmpeg users mailing list. The current status is basically where I left off there. Files produced by ffmpeg don't play correctly (wobbly video)on the ipad but audio stays in sync and files produced by mp4box play well except for audio sync. From what you are saying here, it seems mencoder is a dead end. You seem to a bit of a fan of mp4box. The process I used goes something like this: 1)demux my avi with mp4box MP4Box -aviraw video video.avi MP4Box -aviraw audio video.avi 2) encode ac3 to aac with ffmpeg ffmpeg -i video_audio.ac3 -acodec libfaac -ac 2 -ab 160k -threads 4 video.aac 3) mux all 3 streams into mp4 with mp4box MP4Box -add video_video.h264 -add video.aac -add video_audio.ac3 -fps 25 - nodrop video.mp4 Any thoughts as to how correct the audio sync? Or perhaps the wobbly video with ffmpeg? Or any other method of acheiving this for that matter?
On 2011.07.24 05:05 AM, Phil Sturges wrote:
From what you are saying here, it seems mencoder is a dead end. It's not really much different from FFmpeg since you're just copying video.
You seem to a bit of a fan of mp4box. I go with what works. The lavf MP4 muxer is broken; MP4Box is very reliable.
The process I used goes something like this: 1)demux my avi with mp4box MP4Box -aviraw video video.avi MP4Box -aviraw audio video.avi 2) encode ac3 to aac with ffmpeg ffmpeg -i video_audio.ac3 -acodec libfaac -ac 2 -ab 160k -threads 4 video.aac I doubt you need to demux the audio before converting it. You can put the audio in a container during the conversion too.
3) mux all 3 streams into mp4 with mp4box MP4Box -add video_video.h264 -add video.aac -add video_audio.ac3 -fps 25 - nodrop video.mp4 -nodrop is probably not a good idea unless MP4Box is giving you better results than without it. BTW, MP4Box's default framerate is 25, so can you omit the option if you want.
Also, MP4 doesn't support AC3. MP4Box may accept it, but it will be put in a private stream (you can put pretty much anything in a private stream). I have no idea how many players will even attempt to play AC3 this way. If you want to use that AC3 stream, your best bet is likely a Matroska container. You could also use MPEG-TS.
I doubt you need to demux the audio before converting it. You can put the audio in a container during the conversion too. According to the mp4box documentation direct importing of h264 in avi containers is not supported. That has been my experience also. Is your suggestion here to create a file video.m4a instead of video.aac? -nodrop is probably not a good idea unless MP4Box is giving you better results than without it. BTW, MP4Box's default framerate is 25, so can you omit the option if you want. Nodrop does seem to have some positive effect on audio sync, although not completely fixing it. Quite true about 25fps. Interestingly, I had been using the default build of mp4box from the ubuntu repositories and this seemed to be the behavior. Last night I downloaded the latest build deb from the gpac website and now I can't even force it to 25fps. It keeps reverting to 29.970 even when I explicitly specify 25fps. I have made a
Andrew Berg <bahamutzero8825 <at> gmail.com> post about this on the sourceforge forum about this and awaiting a reply.
Also, MP4 doesn't support AC3. MP4Box may accept it, but it will be put in a private stream (you can put pretty much anything in a private stream). I have no idea how many players will even attempt to play AC3 this way. If you want to use that AC3 stream, your best bet is likely a Matroska container. You could also use MPEG-TS.
The purpose of this little project is to allow playback on iPads and other iOS devices whilst maintaining the ac3 surround sound for playback on my home theatre systems via xbmc. So mp4 is my only option. Any new videos I now encode with handbrake which allows the h264,aac and ac3 streams in the mp4 container. This works great for my purposes, so whether or not or how it is supported doesn't matter too much to me as long as I can get to work, and at this stage it "works", just having audio sync issues. Cheers Phil
On 2011.07.24 07:46 PM, Phil Sturges wrote:
Andrew Berg <bahamutzero8825 <at> gmail.com>
I doubt you need to demux the audio before converting it. You can put the audio in a container during the conversion too. According to the mp4box documentation direct importing of h264 in avi containers is not supported. That has been my experience also. Is your suggestion here to create a file video.m4a instead of video.aac? The video needs to be demuxed to a raw stream, but the audio doesn't. You can feed the AVI to FFmpeg and have it output AAC to an MP4 container (video.m4a).
This works great for my purposes, so whether or not or how it is supported doesn't matter too much to me as long as I can get to work, and at this stage it "works", just having audio sync issues. I suppose if it works, it's not really an issue. I find it surprising that a hardware player supports it, though.
Andrew Berg <bahamutzero8825 <at> gmail.com> writes:
On 2011.07.24 07:46 PM, Phil Sturges wrote:
Andrew Berg <bahamutzero8825 <at> gmail.com>
I doubt you need to demux the audio before converting it. You can put the audio in a container during the conversion too. According to the mp4box documentation direct importing of h264 in avi containers is not supported. That has been my experience also. Is your suggestion here to create a file video.m4a instead of video.aac? The video needs to be demuxed to a raw stream, but the audio doesn't. You can feed the AVI to FFmpeg and have it output AAC to an MP4 container (video.m4a).
This works great for my purposes, so whether or not or how it is supported doesn't matter too much to me as long as I can get to work, and at this stage it "works", just having audio sync issues. I suppose if it works, it's not really an issue. I find it surprising that a hardware player supports it, though.
So if I understand correctly, your suggesting something like: 1) demux h264 from avi with mp4box 2) use ffmpeg to encode ac3 to aac and put it into m4a 3) use mp4box to mux -h264 demuxed from avi -aac from created m4a -ac3 directly from avi Does all that seem feasible? Thanks again Phil
On 2011.07.24 09:31 PM, Phil Sturges wrote:
3) use mp4box to mux -h264 demuxed from avi Yes.
-aac from created m4a
Add the m4a file directly; MP4Box will have no problem with it.
-ac3 directly from avi Does all that seem feasible?
If MP4Box will accept the AC3 that way (I can't remember if it will), then yes.
Andrew Berg <bahamutzero8825 <at> gmail.com> writes:
If MP4Box will accept the AC3 that way (I can't remember if it will), then yes.
Firstly, there was a bug with the recent builds of mp4box that caused it to ignore fps option. That has now been fix. I have been trying to implement the process you have suggested and it all goes well until the final muxing of everything together. The problem is using the ac3 in the avi container. The command I'm using is like so: MP4Box -add video_video.h264:fps=25:nodrop:packed:par=142192:100000 -add video.m4a -add video.avi#audio -new video.mp4 I have tried variations based on the gpac documentation such as video.avi#trackID=2 and video.avi#2. All these seem to add the h264 and the aac/m4a no problem but then fail with the ac3/avi. Perhaps I'm doing something wrong or perhaps it's not supported. The output is below: AVC-H264 import - frame size 704 x 528 at 25.000 FPS Adjusting AVC SizeLength to 16 bits Adjusting AVC SizeLength to 32 bits | (04/100) AVC Import results: 155233 samples - Slices: 1163 I 63301 P 90769 B - 0 SEI - 1133 IDR IsoMedia import - track ID 1 - Audio (SR 48000 - 2 channels) Unsupported AVI audio format Error importing /storage/scratch/movietest/video.avi#audio: Feature Not Supported Perhaps its time to move this discussion to a more appropriate forum. This has gotten way off the subject of mencoder. I'll probably ask further questions on the gpac sourceforge forum. Cheers Phil
On Thu, Jul 28, 2011 at 12:02:27AM +0000, Phil Sturges wrote:
I have tried variations based on the gpac documentation such as video.avi#trackID=2 and video.avi#2. All these seem to add the h264 and the aac/m4a no problem but then fail with the ac3/avi. Perhaps I'm doing something wrong or perhaps it's not supported. The output is below:
Tried e.g. mplayer -dumpaudio to extract the raw AC3 and mux that in?
participants (3)
-
Andrew Berg -
Phil Sturges -
Reimar Döffinger