Log in

View Full Version : piping mencoder output to x264


darkpingu
8th January 2009, 01:31
Hi all,

I'm trying to pipe out mencoder output so I can encode the video with x264.exe

This is the command I am using (on Windows XP pro spanish):

mencoder -nosound -ovc raw _base_.avi -o - -really-quiet | x264 - 640x480 -o _base_x264ed_.avi -B 1200

mencoder and x264 are both in the PATH

Ok, so this *seems* to work but the resulting video looks quite weird. It is kind difficult for me to describe so I have uploaded It and You may take a look to the original and the x264ed one.

The url is:
http://rapidshare.com/files/180880585/Example.zip.html
I cutted the video so It does not weight too much.

Any ideas why this is happening? How can It be solved? Am I doing somethin wrong?

Thanks in advance and excuse my poor English.

ACoolie
8th January 2009, 02:01
try -vf format=i420 -ofvideo rawvideo

Selur
8th January 2009, 10:02
1.
Ok, so this *seems* to work but the resulting video looks quite weird.
Color space problems. :)
Depending on the input you might have to use "-vf format=yuy2,scale,format=i420" or "-vf format=i420" to make sure mencoder decodes into the correct color space without breaking something +
an additional" -vfm ffmpeg" is needed sometimes.

for dv video it's sometimes better to use ffmpeg instead of mencoder, here's how I handle your source in sx264 to pipe to x264:
ffmpeg -i "_base_.avi" -v 0 -vcodec rawvideo -vsync 0 -an -pix_fmt yuv420p -f rawvideo - | x264 ...

2. It's not recommend to use the .avi container for x264 output.

Cu Selur

Dark Shikari
8th January 2009, 15:54
Or just use yuv4mpeg... it's there for a reason ;)

mkfifo pipe.y4m;x264 pipe.y4m <options> & mplayer -benchmark -vo yuv4mpeg:file=pipe,y4m -ao null

vmrsss
8th January 2009, 16:43
DS, what does -benchmark do for you? I normally use

ffmpeg -i input_file "-crop and -s commands here" -f yuv4mpegpipe -y stream.y4m

or less frequently, when I want some filter, eg yadif

mplayer input_file -vf "various_filters including crop and scale",harddup -vo yuv4mpeg:file=stream.y4m

where stream.y4m is a mkfifo pipe.

Dark Shikari
8th January 2009, 16:48
DS, what does -benchmark do for you?I think it automatically sets mplayer so that it outputs at full speed rather than 1x realtime?

roozhou
9th January 2009, 05:13
@DS

This is the command I am using (on Windows XP pro spanish):


mkfifo pipe.y4m;x264 pipe.y4m <options> & mplayer -benchmark -vo yuv4mpeg:file=pipe,y4m -ao null

Have you ever used Windows cmd? There is no mkfifo on Win32. The one in MinGW doesn't work.
The mechanisms of named pipe on *nix and Windows are entirely different.

Dark Shikari
9th January 2009, 14:08
Have you ever used Windows cmd? There is no mkfifo on Win32.Sure there is. We call it "Cygwin."

roozhou
9th January 2009, 18:13
Sure there is. We call it "Cygwin."
Well, why do you always refuse to use native windows api but prefer a POSIX wrapper(e.g. Cygwin, pthread)?

Look at this post, it is not that difficult.
http://forum.doom9.org/showthread.php?p=1226337#post1226337

Shinigami-Sama
10th January 2009, 06:43
Well, why do you always refuse to use native windows api but prefer a POSIX wrapper(e.g. Cygwin, pthread)?

Look at this post, it is not that difficult.
http://forum.doom9.org/showthread.php?p=1226337#post1226337

why support something that everyone knows is a joke?
why do you think microsoft keeps making new shells and scripts?

roozhou
10th January 2009, 09:29
why support something that everyone knows is a joke?
why do you think microsoft keeps making new shells and scripts?
Do you mean native OS api is a joke and M$ should replace it with POSIX api in next generation of Windows?

Why not spend a few more minutes on MSDN? You can easily find solutions there.

Shinigami-Sama
10th January 2009, 09:30
Do you mean native OS api is a joke and M$ should replace it with POSIX api in next generation of Windows?

Why not spend a few more minutes on MSDN? You can easily find solutions there.

you obviously didn't read what I said very well...

roozhou
10th January 2009, 09:46
you obviously didn't read what I said very well...
Sorry, you mean cmd.exe is a joke? I agree with you.

Well, back to the mkfifo issue. The problem is Windows does not natively supports named pipes that can be accessed by name on BOTH ends. If the shell needs to implement a UNIX-like mkfifo, it has to create two pipes and manually move data from one pipe to another, resulting in one more pair of ReadFile/WriteFile, and of course with bad performance.

darkpingu
12th January 2009, 15:28
Hi,

thank You all. Adding -vf format=i420 -ofvideo rawvideo to mencoder does the trick




for dv video it's sometimes better to use ffmpeg instead of mencoder, here's how I handle your source in sx264 to pipe to x264:


Please, could you explain why this statement?



Oh... and Cygwin could be a good idea but in this case I want to deliver to some friends a simple script so they can compress some videos easily. So installing cygwin on every computer is not a perfect solution.

Selur
10th February 2009, 11:38
Please, could you explain why this statement?
For some dv video sources mencoder gave me a b0rked output where ffmpeg did not so I stay with ffmpeg as decoder.

Cu Selur

_DW_
10th February 2009, 16:39
Oh... and Cygwin could be a good idea but in this case I want to deliver to some friends a simple script so they can compress some videos easily. So installing cygwin on every computer is not a perfect solution.

Why are you using mencoder if you want to keep it simple? If your using windows it seems you would want to use a gui based encoder like staxrip or mgui? That way you could just pass around a simple profile and not have to worry about things like mkfifo and named pipes.

JohannesL
10th February 2009, 20:54
MEncoder can encode with x264 just fine. -ovc x264 -x264encopts crf=22:frameref=5:bframes=3 -of rawvideo -o file.264
Assuming MEncoder is compiled with x264.. Up-to-date builds can be found at http://oss.netfarm.it/mplayer-win32.php

bigtoedsloth
15th August 2009, 14:30
Or just use yuv4mpeg... it's there for a reason ;)

mkfifo pipe.y4m;x264 pipe.y4m <options> & mplayer -benchmark -vo yuv4mpeg:file=pipe,y4m -ao null


Is there a way to automatically maintain a/v sync while encoding video and audio streams separately like this?

The mencoder way is to set "-oac copy". But I've tried "-ao null" for mplayer, in a command like the above, with no luck.

I've been trying to transcode a DVD following the suggestions here (http://sites.google.com/site/linuxencoding/x264-encoding-guide), but have run into this a/v sync issue.

multimediaman
15th August 2009, 15:24
run into this a/v sync issue.
-vf harddup ?

bigtoedsloth
15th August 2009, 22:59
harddup is certainly something I'll have to include in my filter list, but there's another problem.

I think that there is a time offset between the audio and video tracks? It must be specified in the original container, but when the audio and video tracks are split out that information is lost. It's been my experience that mencoder deals with it by skipping video frames to keep sync. I'd like to know if it's possible to keep sync using the above mplayer output to x264 + mplayer output to some audio encoder + mux.

Under linux, which I didn't mention before ...

bitbytebit
16th August 2009, 06:21
often mpeg or other media can have an offset for audio/video, especially after being chopped. You have to either drop or duplicate video frames to get it back into sync, usually just at the startup. FFmpeg does this with -async 1 -vsync 1 options, mencoder/mplayer do it through -mc 1 (by default it's less than 1 though, -mc 0 turns it off). So you'll either have to do it manually, writing your own frame server into the mux, or let ffmpeg/mencoder (or other) do it for you. Unless your video/audio is perfect frame for frame in sync, this has to be done, and unless you enjoy writing complex code to sync the pts/dts values of the audio/video by dropping/adding video frames, the most reliable way is ffmpeg or mencoder (I prefer ffmpeg myself). Also you can decode the audio/video with ffmpeg using the -async 1 -vsync 1 options, and pipe that output to x264 and an audio encoder. This will also work, because ffmpeg will do the syncing still even when decoding each stream separate to raw yuv/pcm if you include those two sync options.

bigtoedsloth
17th August 2009, 01:27
That sounds great. I'm not familiar with ffmpeg, so to be explicit (borrowing from vmrsss earlier in this thread)

ffmpeg -i input_file "-crop and -s commands here" -async 1 -vsync 1 -f yuv4mpegpipe -y video.y4m -f wav -y audio.wav

should do what I want? I mean if video.y4m and audio.wav are both named pipes and they are encoded independently with external encoders, then when I mux them they will be in sync?

bitbytebit
17th August 2009, 06:05
Something like this is what I have found works: (you must put the -async 1 -vsync 1 options before the -i input_file), you want to actually demux the source with ffmpeg in two commands like this, otherwise there will be no way for the demuxing to be able to know what to do for dup/drop frames to keep them in sync.

mkfifo adec.pcm
mkfifo vdec.yuv
ffmpeg -async 1 -vsync 1 -i input_file -y -f s16le -acodec pcm_s16le -ar 32000 -ac 1 -vn adec.pcm
ffmpeg -async 1 -vsync 1 -i input_file -y -f rawvideo -r 16 -vcodec rawvideo -pix_fmt yuv420p -s 320x240 -aspect 4:3 -an vdec.yuv

faac -P -R 32000 -C 1 -X -b 32 --mpeg-vers 4 -o aenc.aac adec.pcm

x264 --no-psnr --8x8dct --aud --mixed-refs --weightb --bitrate 100 --aq-mode 1 --aq-strength 1.0 --deblock 0:0 --fps 16 --threads auto --sar 1:1 --min-keyint 16 --trellis 0 --subme 6 --direct auto --psy-rd 0.0,0.0 -r 4 --me hex --partitions "all" --bframes 16 --keyint 32 --merange 16 --qcomp 0.60 -o venc.264 venc.yuv 320x240
MP4Box -inter 1000 -fps 16 -add venc.264#video -add aenc.aac#audio -new output.mp4

rm aenc.aac venc.264


Of course this can be totally changed around to do different things and use different encoders, muxer. This is just a working full processing that I've used and it keeps things in sync even when there's an a/v offset to the source files. I've never found a way to have mencoder/mplayer to do the same magic at demuxing while doing the sync stuff, it won't do it when demuxing like ffmpeg, I tried and never could do this with it unless doing the whole process with mencoder only.

Selur
17th August 2009, 08:12
When you want to transcode a dvd why not use mencoder which supports dvd input and not just single files like ffmpeg? (I'm using mencoder in sx264 and it works fine)

bigtoedsloth
18th August 2009, 00:55
When you want to transcode a dvd why not use mencoder which supports dvd input and not just single files like ffmpeg? (I'm using mencoder in sx264 and it works fine)

I've previously used mencoder, but I'm keen on using the preset and tune flags in x264. Hence piping the output from one tool (mplayer, mencoder or ffmpeg) to x264.

multimediaman
18th August 2009, 07:13
You can create your own profiles for mencoder based on x264 tunings.

http://www.mplayerhq.hu/DOCS/man/en/mplayer.1.html#PROFILES

Probably you can not override profile settings though. :(

qyot27
18th August 2009, 13:37
You can create your own profiles for mencoder based on x264 tunings.

http://www.mplayerhq.hu/DOCS/man/en/mplayer.1.html#PROFILES

Probably you can not override profile settings though. :(
I never actually got multiple profiles working correctly on my end, but overrides to the settings in mencoder.conf are definitely possible. I do it all the time when telling mencoder I want sound in the output file or want to encode with Xvid. By default I have it set up for no sound and to encode to ffvhuff. All you have to do is call those options like you'd have to if the profile wasn't set.

multimediaman
18th August 2009, 14:37
I never actually got multiple profiles working correctly on my end, but overrides to the settings in mencoder.conf are definitely possible.

Yep to some extent:

Here is my test:

[x264something]
of=rawvideo=1
ovc=x264=1
x264encopts=crf=18:frameref=5:bframes=5:aq_mode=2
nosound=1

Plain profile
mencoder test.mkv -profile x264something -o test_1.264 -endpos 5

Result:

cabac=1 / ref=5 / deblock=1:0:0 / analyse=0x3:0x113 / me=hex / subme=7
/ psy=1 / psy_rd=1.0:0.0 / mixed_ref=1 / me_range=16 / chroma_me=1 / trellis=1 / 8x8dct=1 / cqm=0 / deadzone=21,11 / chroma_qp_offset=-2 / threads=1 / nr=0 / decimate=1
/ mbaff=0 / bframes=5 / b_pyramid=0 /
b_adapt=1 / b_bias=0 / direct=1 / wpredb=1 / keyint=250 / keyint_min=25 / scenecut=40 /
rc_lookahead=40 / rc=crf / mbtree=1 / crf=18.0 / qcomp=0.60 / qpmin=10 / qpmax=51 / qpstep=4 / ip_ratio=1.40 / aq=2:1.00

Let's override some x264settings and profile settings
mencoder test.mkv -profile x264something -x264encopts crf=19:trellis=2:b_adapt=2:frameref=1 -o test_2.264 -endpos 5
Result:
cabac=1 / ref=5 / deblock=1:0:0 / analyse=0x3:0x113 /
me=hex / subme=7 / psy=1 / psy_rd=1.0:0.0 / mixed_ref=1 / me_range=16 / chroma_me=1 / trellis=2 / 8x8dct=1 / cqm=0 / deadzone=21,11 / chroma_qp_offset=-2 / threads=1 / nr=0 /
decimate=1 / mbaff=0 / bframes=5 / b_pyramid=0 / b_adapt=2 / b_bias=0 / direct=1 / wpredb=1 / keyint=250 / keyint_min=25 / scenecut=40 / rc_lookahead=40 / rc=crf / mbtree=1 /
crf=18.0 qcomp=0.60 / qpmin=10 / qpmax=51 / qpstep=4 / ip_ratio=1.40 / aq=2:1.00

So you can not change for example profile crf if it is included in it. However it is possible to change x264 defaults that are not included in profile.

qyot27
18th August 2009, 16:37
Ah ha, I've found the issue. It will override settings included there, if you don't define profiles in the first place. In other words, if [x264something] is there, then overrides can't work. If that's the only profile set in mencoder.conf, then the bracketed names are totally unnecessary and taking them out will allow the overrides to go through. This was probably the issue that made me say I couldn't get multiple profiles working (as this was many many months ago, and I'd not tried since - makes sense, as my ffvhuff configuration doesn't have a profiled name).

mencoder.conf:
of=rawvideo=true
ovc=x264=true
x264encopts=crf=18:frameref=5:bframes=5:aq_mode=2
nosound=true

mencoder test.avs -o test.264
cabac=1 ref=5 deblock=1:0:0 analyse=0x1:0x111 me=hex subme=6 psy_rd=1.0:0.0 mixed_ref=0 me_range=16 chroma_me=1 trellis=0 8x8dct=0 cqm=0 deadzone=21,11 chroma_qp_offset=-2 threads=1 nr=0 decimate=1 mbaff=0 bframes=5 b_pyramid=0 b_adapt=1 b_bias=0 direct=1 wpredb=0 keyint=250 keyint_min=25 scenecut=40 rc=crf crf=18.0 qcomp=0.60 qpmin=10 qpmax=51 qpstep=4 ip_ratio=1.40 pb_ratio=1.30 aq=1:1.00

mencoder test.avs -o test.264 -x264encopts crf=19:trellis=2:b_adapt=2:frameref=1
cabac=1 ref=1 deblock=1:0:0 analyse=0x1:0x111 me=hex subme=6 psy_rd=1.0:0.0 mixed_ref=0 me_range=16 chroma_me=1 trellis=2 8x8dct=0 cqm=0 deadzone=21,11 chroma_qp_offset=-2 threads=1 nr=0 decimate=1 mbaff=0 bframes=5 b_pyramid=0 b_adapt=2 b_bias=0 direct=1 wpredb=0 keyint=250 keyint_min=25 scenecut=40 rc=crf crf=19.0 qcomp=0.60 qpmin=10 qpmax=51 qpstep=4 ip_ratio=1.40 pb_ratio=1.30 aq=1:1.00





So basically, if you want to have slight variations in your profiles, they've all got to be defined beforehand, nothing on-the-fly. Unless of course I'm missing some specific user-override option that needs to be passed as well. But if you only have one configuration setup, omit the naming convention and then you can override anything in there to your heart's content.

bigtoedsloth
19th August 2009, 13:23
Something like this is what I have found works:

Cheers bitbytebit! That did the trick. Thanks for your help.

Thanks also to the others that gave their advice, it's been an excellent first posting experience at doom9.