Log in

View Full Version : Guru's help needed, problem with joining two files


AlexeyS
14th January 2008, 10:37
Hello,

I've killed 2 month to do this, but I still can't solve the problem. It was pain, I started to hate Linux now. :( I need to join two video files on Linux server without X installed.

I've tried mencoder, ffmpeg, avimerge, but I video I get is without audio sync.

For example, when I encode second video with mencoder (to FLV), a/v sync is ok. But when I encode both files to MPEG4/MP3 (to join them later):
mencoder -vf scale=448:-3,expand=448:336 -sws 9 -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=4000 -frames 1000 -oac mp3lame -lameopts cbr:br=320:mode=3 -channels 1 -o test2.avi test07g.avi

mencoder -vf scale=448:-3,expand=448:336 -sws 9 -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=4000 -oac mp3lame -lameopts cbr:br=320:mode=3 -channels 1 -o test1.avi logo7aaa.avi

and join them with mencoder or avimerge:

mencoder -ovc copy -oac copy -o test.avi test1.avi test2.avi

avimerge -o test.avi -i test1.avi test2.avi

I get video where audio delay is about 1 second. :(

I wonder what am I doing wrong?

microchip8
14th January 2008, 11:42
you need to add the harddup filter at the end of the filter chain so that mencoder will hardcode duplicate frames and not write zero's in the video streams

-vf scale=448:-3,expand=448:336,harddup

AlexeyS
14th January 2008, 16:02
you need to add the harddup filter at the end of the filter chain so that mencoder will hardcode duplicate frames and not write zero's in the video streams

-vf scale=448:-3,expand=448:336,harddup
Thanks for quick reply, froggy. I've added ,harddup when I encode two clips to MPEG4, but after I join them I still get video with a/v sync problem.

The thing is when I use first 2 commands to encode 2 clips, I get those clips without a/v sync problem.

The problem appears only after I join clips with:

mencoder -ovc copy -oac copy -ignore-start -o test.avi test1.avi test2.avi
OR
avimerge -o test7.avi -i test1.avi test2.avi


Is there anything else, I could try?

talen9
14th January 2008, 16:33
Try adding "-vf harddup" on the command line used for joining too :)

Reimar
14th January 2008, 16:45
Try adding "-vf harddup" on the command line used for joining too :)

any -vf can not work with -ovc copy. You can try -mc 0 -noskip, but I'm not sure if concatenating two files without reencoding can work reliable, it is difficult enough even when reencoding.

microchip8
14th January 2008, 16:47
Try adding "-vf harddup" on the command line used for joining too :)

video filters do not work when one copies the video stream. Same thing for audio filters...

what he can do though is try with the -mc 0 -noskip options

EDIT: Reimar was faster than me :)

AlexeyS
14th January 2008, 17:17
Thanks guys. But it doesn't work too:

mencoder -mc 0 -noskip -ovc copy -oac copy -o test.avi test1.avi test2.avi


Maybe I'm doing wrong when I'm encoding files to MPEG4/MP3 to join them? Maybe I should try something else (MPEG2 or maybe some lossless codec)?

Also, if you have Mencoder installation on your computer or server, could you try my clips please?

http://www.roamearth.com/ff/logo7aaa.avi
http://www.roamearth.com/ff/test07g.avi

Maybe I'm doing wrong something else.

microchip8
14th January 2008, 17:37
first link has a permissions error

You don't have permission to access /ff/logo7aaa.avi on this server.

nm
14th January 2008, 17:50
Joining test07g.avi to itself worked fine for me, so maybe the problem is in the other file.

However, as Reimar already said, it would probably be more reliable to concatenate uncompressed video and audio streams and re-encode the resulting streams. You don't need to store the uncompressed video to do that--just pipe the frames to an encoder process in the desired order.

AlexeyS
14th January 2008, 18:04
first link has a permissions error

You don't have permission to access /ff/logo7aaa.avi on this server.
Sorry, fixed.

Joining test07g.avi to itself worked fine for me, so maybe the problem is in the other file.
Can you check another file please.

However, as Reimar already said, it would probably be more reliable to concatenate uncompressed video and audio streams and re-encode the resulting streams. You don't need to store the uncompressed video to do that--just pipe the frames to an encoder process in the desired order.
I've tried using pipes by examples, but I completely do not understand it.

I wonder what uncompressed codecs (video and audio) should I use? I've tried huffyuv (doesn't work), ffvhuff (works, but I get video noise instead of video), ffv1 (same as ffvhuff, just video noise).

AlexeyS
15th January 2008, 12:24
Anyone please?

nm
15th January 2008, 16:05
You have a framerate problem: the first file is 25 fps video and the second specifies a rate of 23.975 fps. MEncoder probably uses 25 fps from the first file and therefore audio will get out of sync pretty soon.

Find out the framerate and frame resolution of the main video--for example by parsing mplayer -identify (or TOOLS/midentify) output--and force them when encoding the files together.

For example, this works for me:
mencoder -fps 24000/1001 -vf scale=480:-2,crop=480:204 -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=4000 -oac mp3lame -noskip -mc 0 -o out.avi logo7aaa.avi test07g.avi

AlexeyS
15th January 2008, 18:32
You have a framerate problem: the first file is 25 fps video and the second specifies a rate of 23.975 fps. MEncoder probably uses 25 fps from the first file and therefore audio will get out of sync pretty soon.

Find out the framerate and frame resolution of the main video--for example by parsing mplayer -identify (or TOOLS/midentify) output--and force them when encoding the files together.

For example, this works for me:
mencoder -fps 24000/1001 -vf scale=480:-2,crop=480:204 -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=4000 -oac mp3lame -noskip -mc 0 -o out.avi logo7aaa.avi test07g.avi

Awesome! Thanks nm. That was my dream to make work everything in just one CLI command.

I have two little problems here, could you help me with these please? I really want to finish this and start video upload feature on my site.

The first problem is I get white bars instead of black when I use this:

mencoder -fps 24000/1001 -vf scale=448:-3,expand=448:336 -sws 9 -ovc lavc -lavcopts vcodec=flv:vbitrate=300 -frames 800 -oac mp3lame -lameopts abr:br=80:mode=0 -noskip -mc 0 -o test2.flv logo7aaa.avi test07g.avi

I know I could crop, but I can't, because those videos are for YouTube, so I can use only 448x336 resolution.

I wonder how can I fix this?

Another problem is I need to transfer video stream to 15 FPS, but when I add -ofps 15 to this command, sound plays like 2x faster:

mencoder -fps 24000/1001 -vf scale=448:-3,expand=448:336 -sws 9 -ovc lavc -lavcopts vcodec=flv:vbitrate=300 -frames 800 -oac mp3lame -lameopts abr:br=80:mode=0 -noskip -mc 0 -o test2.flv logo7aaa.avi test07g.avi

I guess these are not big problems, but I'm newbie at mencoder.

nm
15th January 2008, 19:15
The first problem is I get white bars instead of black when I use this:
That's a bug (http://lists.mplayerhq.hu/pipermail/mplayer-advusers/2006-May/001422.html) in MEncoder. I didn't try recent SVN versions though, so it might be fixed now (probably not).

A workaround would be to have a black frame at the end of the logo video, or read in a third video clip with one black frame between the logo and the main video.

Another problem is I need to transfer video stream to 15 FPS, but when I add -ofps 15 to this command, sound plays like 2x faster:
With MEncoder, you can only do that by dropping frames or by slowing playback down (see -speed option). The latter is not useful in this case because the difference between 15 fps and film framerates is too great. To drop frames, remove "-noskip -mc 0" from the command line.

Edit: Also note that those command lines won't output video in FLV container. MEncoder outputs AVI by default and to change that, you need to use the -of option. I'm not sure if -of lavf -lavfopts format=flv will output a correct file, but if it doesn't, you could use ffmpeg to remux MEncoder's AVI output.

AlexeyS
15th January 2008, 19:35
That's a bug (http://lists.mplayerhq.hu/pipermail/mplayer-advusers/2006-May/001422.html) in MEncoder. I didn't try recent SVN versions though, so it might be fixed now (probably not).
I use 1 day old SVN, so they didn't fix it. :(

A workaround would be to have a black frame at the end of the logo video, or read in a third video clip with one black frame between the logo and the main video.
I think I will be able to add one black frame to the logo AVI. Thanks for the idea.

With MEncoder, you can only do that by dropping frames or by slowing playback down (see -speed option). The latter is not useful in this case because the difference between 15 fps and film framerates is too great. To drop frames, remove "-noskip -mc 0" from the command line.
What do you mean by "not useful"? Will movie look buggy or something?

Edit: Also note that those command lines won't output video in FLV container. MEncoder outputs AVI by default and to change that, you need to use the -of option. I'm not sure if -of lavf -lavfopts format=flv will output a correct file, but if it doesn't, you could use ffmpeg to remux MEncoder's AVI output.
Thanks I will try that.

nm
15th January 2008, 19:49
What do you mean by "not useful"? Will movie look buggy or something?
I mean that lowering the playback speed would make it look and sound much too slow--like a continuous slow-motion scene. Dropping frames doesn't cause this, but the result will be a bit more jerky than what 15 fps video could optimally be, due to the uneven framerate ratio.

Shortly put, if you wan't 15 fps output when encoding 20+ fps video with MEncoder, allow it to drop frames.

AlexeyS
15th January 2008, 20:08
I mean that lowering the playback speed would make it look and sound much too slow--like a continuous slow-motion scene. Dropping frames doesn't cause this, but the result will be a bit more jerky than what 15 fps video could optimally be, due to the uneven framerate ratio.

Shortly put, if you wan't 15 fps output when encoding 20+ fps video with MEncoder, allow it to drop frames.
So, I must to encode them spearately to some loseless (at 15 fps) first with allowed frame drop. And then join. Ok, I can do that.

2 questions. What loseless codec should I use to be able join two files without any problems (MPEG4 does this, but it's far from loseless)?

And the other one is about FLV format. I've tried you options but I get error message "unknown format". I've googled and found "wroking command line" with -ffourcc FLV1 option. But the thing is when I play it with VLC, I can't see video. VLC says that can't find video decoder for "undf" ffourcc.

AlexeyS
15th January 2008, 20:24
Probably the best option will be to encode videos separately, join them and then re-encode with ffmpeg.

But can you advice me some lossless codec please, which will work with ffmpeg and will work fine for joining videos?

nm
15th January 2008, 20:30
So, I must to encode them spearately to some loseless (at 15 fps) first with allowed frame drop. And then join. Ok, I can do that.
No need to do that. Use the same command line as before but leave -noskip -mc 0 out, as I suggested:mencoder -fps 24000/1001 -vf scale=448:-3,expand=448:336 -sws 9 -ovc lavc -lavcopts vcodec=flv:vbitrate=300 -frames 800 -oac mp3lame -lameopts abr:br=80:mode=0 -ofps 15 -o test2.avi logo7aaa.avi test07g.avi

And the other one is about FLV format. I've tried you options but I get error message "unknown format". I've googled and found "wroking command line" with -ffourcc FLV1 option. But the thing is when I play it with VLC, I can't see video. VLC says that can't find video decoder for "undf" ffourcc.
Hmm. I just tried with old MEncoder r22005 and FLV output through lavf seems to work: at least MPlayer and VLC play the resulting file. I just added this to your command line before the -o option (newer versions don't need that i_certify_... part):-of lavf -lavfopts format=flv:i_certify_that_my_video_stream_does_not_use_b_frames
If I add -ffourcc FLV1, I get the same error message as you about "unknown format" from VLC and MPlayer.

AlexeyS
15th January 2008, 21:40
Nice. :) It works.

The last thing. As you said video becomes jerky in scenes with fast motion (static scenes are ok). Is there any way to fix it?

If not, what would happen if I use 24 fps for videos (even for 30 fps videos)?

PS: Video is really jerky (movement at 54 second, for example):
http://ru.youtube.com/watch?v=tX68SxcFNQo

nm
15th January 2008, 23:16
If not, what would happen if I use 24 fps for videos (even for 30 fps videos)?
It's always best to use the original framerate and I don't think YouTube restricts that as long as it's 30 fps or lower and you obey the bitrate limit (or hack the stream (http://forum.videohelp.com/topic336882.html)).

AlexeyS
15th January 2008, 23:35
It's always best to use the original framerate and I don't think YouTube restricts that as long as it's 30 fps or lower and you obey the bitrate limit (or hack the stream (http://forum.videohelp.com/topic336882.html)).
Ok, I will do that. Thanks a lot for everything.

You made me very very happy. :)

nm
15th January 2008, 23:49
You're welcome :)

echo
16th January 2008, 16:32
I guess you could join the files into a single matroska file with mkvmerge. From the on you could split that where you want with mkvmerge again and finally use mkvextract to create the split avi files. Or I guess "mencoder -ovc copy -oac copy" along with the use of -ss and -endpos would create two avi files out of the joined mkv file.