iAudiophile.net Forums
Go Back   iAudiophile.net Forums > Flash-Memory Players > Cowon D2/D2+ > D2 Videos & Recordings

Post New Thread  Reply
 
Thread Tools Display Modes
Old 06-05-2007, 13:44   #1
Dase
Junior Member
 
Join Date: May 2007
Posts: 11
Default Using ffmpeg.exe for command-line encoding and great A/V sync

First of all, I want to qualify that ffmpeg.exe with the parameters I give below works for me. I can encode a 3-hour movie with perfect sync and skipping around within the movie on my D2 doesn't make it lose sync.

Here's my situation. My DirecTV TiVo records all my shows. I need to get them on my D2, so I needed something that would take the TiVo semi-MPEG2 format files and convert them to XVID. DirecTV TiVo files are tricky to work with since they're delivered by satellite and they tend to have occasional missing frames and other glitches.

I'm a big fan of mencoder, but nothing I did in mencoder gave me decent A/V sync. Sync would get worse the longer the show played.

That's when I learned about ffmpeg, a DOS command-line encoder/converter like mencoder. What makes ffmpeg special? The -async command. Here's the description of -async from the ffmpeg documentation:

"Audio sync method. "Stretches/squeezes" the audio stream to match the timestamps, the parameter is the maximum samples per second by which the audio is changed. -async 1 is a special case where only the start of the audio stream is corrected without any later correction."

The async command fixed my sync problems. Generally, using -async 1 works well enough and is the least cpu-intensive choice. If you're still having sync problems, you can use something like -async 44100. Using something other than 1 allows ffmpeg to dynamically slow down or speed up the audio to make sure it syncs with the video.

How to use ffmpeg

You can get the latest version of ffmpeg here:

http://ffdshow.faireal.net/mirror/ffmpeg

Just grab the latest version. Note that they're in 7-zip format. You'll need winrar or 7-zip (http://www.7-zip.org/). There's no install. Just unzip it somewhere logical on your hard drive.

I do two-pass encodes for quality. To automate this with a batch file, copy the content between the batch begin and end lines to notepad and save it as ffencode.bat or whatever you want.

To use this batch file, make sure it's in the path and that ffmpeg.exe is in your path too (or include the path to ffmpeg.exe in the batch file). Then, type this from a command prompt:

ffencode <myfile.avi>

Where <myfile.avi> is the file you want to encode.

----------Batch begin----------

@Echo Starting pass 1
FFMPEG -i %1 -pass 1 -s 320x240 -vcodec mpeg4 -vtag XVID -b 500kb -mbd rd -flags +4mv+trell+aic -cmp 2 -subcmp 2 -g 300 -acodec copy null.avi

@Echo Starting pass 2
FFMPEG -i %1 -pass 2 -s 320x240 -vcodec mpeg4 -vtag XVID -b 500kb -mbd rd -flags +4mv+trell+aic -cmp 2 -subcmp 2 -g 300 -acodec mp3 -ab 128 -ac 2 -async 1 D2-%1

@Echo Done, deleting temporary files
del null.avi
del ffmpeg2pass-0.log

----------Batch end------------

Here's the basic command-line breakdown

-i %1
This is the input file that you passed on the batch file command line.

-s 320x240 -vcodec mpeg4 -vtag XVID
This sets the output image size. You can change image size if necessary. vcodec and vtag set the encoding and identifier to XVID.

-b 500kb
This sets the bit rate. I like 500kb/sec. You can change this to whatever you like.

-mbd rd -flags +4mv+trell+aic -cmp 2 -subcmp 2 -g 300
This improves the default quality settings by changing the motion search precision and so on. These standard values are recommended in the ffmpeg FAQ. Set the -g parameter to 10x your source video framerate. I mostly encode 30fps material so I use -g 300.

Here are the pass 1 differences:

pass 1
This tells ffmpeg to generate a log file with quality information. The second pass will use the data from this file for the final encode.

-acodec copy null.avi
Since the first pass only analyzes video we tell ffmpeg to just copy the source audio and the first pass encode to null.avi. Null.avi isn't used after the first pass and can be deleted.

Here are the pass 2 differences:

pass 2
This tells ffmpeg to use the log from the first pass.

-acodec mp3 -ab 128 -ac 2 -async 1 D2-%1
This sets the audio codec to MP3 with an average bitrate of 128kb/sec. -ac 2 sets the mp3 audio to 2-channel joint stereo. -async 1 is critical and is discussed above. D2-%1 is the output file name. This prefixes the source filename with D2-.

Note, if you have a multicore CPU you can increase the encoding speed by including this parameter on the command lines:

-threads 2

This will decrease quality a tiny bit.

I hope these settings work for you. This is the only way I'm able to get good A/V sync.

Last edited by Dase; 06-05-2007 at 15:02..
Dase is offline View Dase's Photo Album   Reply With Quote
Old 06-06-2007, 03:16   #2
Aesculapius
Junior Member
 
Join Date: Jun 2007
Posts: 18
Default

Thanks for the tip!

I expanded the script a little bit to use two threads anyway (you won't notice the quality diffs and it works on a 1cpu system too) plus - what came in handy for myself - allow it to have a path, so you can have your videofile in another directory than the ffmpeg program.
Also the number 300 is an optional parameter...and I labeled stuff a bit clearer.

call it the same as the original script, though you can add the 10-times-framerate number and use paths, like

ffencode "E:\otherdir\The West Wing - S1 E04 - Five Votes Down.avi" 300

Make sure you watch the quotes when you have spaces in it
Attached Files
File Type: txt ffencode.bat.txt (1.3 KB, 113 views)

Last edited by Aesculapius; 06-06-2007 at 18:18.. Reason: expanded script
Aesculapius is offline View Aesculapius's Photo Album   Reply With Quote
Old 06-07-2007, 11:59   #3
Zverik
Member
 
Join Date: May 2007
Posts: 42
Default

I've checked this. A/V sync is perfect (fw 2.46). This is rather strange.
Does mencoder have a similar command?
Is it possible to encode subtitles with ffmpeg?
Zverik is offline View Zverik's Photo Album   Reply With Quote
Old 06-07-2007, 12:53   #4
Maxwell Smart
Member
 
Join Date: May 2007
Posts: 35
Default

Holy crap, Dase, this really makes a major positive difference.

The video quality is tons better -- while I kicked the bitstream up from 400 kb/s to 512, I can tell from the relative lack of artifacts around credit titles, the 2 pass method and your other flags likely have more to do with it.

Anyway, I can skip around Raiders of the Lost Ark, from the end of the movie back to the beginning, to the middle, whatever -- A/V sync stays good.

You rock, Dase!

I'm including a shell script version for the Linux geeks around here. This works fine on Debian etch. Note that you have to have a version of ffmepg installed that will use lame to encode mp3 audio. Because MP3 is patent-encumbered, the version of ffmpeg in official Debian distribution channels lacks this. Try compiling from source or using Christian Marillat's repository.

But shell is shell (esp. when one takes pains to write in the POSIX-standard dialect, as I do) and this should work on any GNU/Linux distro.

Code:
#!/bin/sh

set -e

PROGNAME=${0##*/}

if [ -z "$1" ]; then
    echo "usage: $PROGNAME VIDEO_FILE" >&2
    exit 1
fi

OUTFILE=${1%.*}.avi

if [ -e pass1.avi ]; then
    echo "$PROGNAME: pass1.avi already exists" >&2
    exit 2
fi

set -x

# -i                    input file
# -pass 1               first pass
# -s 320x240            maximum resolution supported by D2
# -vcodec mpeg4         use MPEG-4 video codec
# -vtag XVID            tell the D2 it's an XviD file
# -b 512                set video stream bitrate to 512 kb/sec
# -mbd 2                use rate distortion macroblock decision
# -flags +4mv+trell+aic stuff recommended by ffmpeg FAQ
# -cmp 2                "
# -subcmp 2             "
# -gop 300              set GOP size to 300 frames (framerate * 10)
# -acodec copy          skip transcode of audio on first paass
ffmpeg -i "$1" -pass 1 -s 320x240 -vcodec mpeg4 -vtag XVID -b 512 -mbd 2 \
    -flags +4mv+trell+aic -cmp 2 -subcmp 2 -g 300 -acodec copy pass1.avi

# -acodec mp3           transcode audio to MPEG-1 layer 3
# -ab 128               set audio stream bitrate to 128kb/sec
# -ac 2                 use 2 audio channels (stereo)
# -async 1              correct beginning of audio stream to sync with video
ffmpeg -i "$1" -pass 2 -s 320x240 -vcodec mpeg4 -vtag XVID -b 512 -mbd 2 -flags
    -flags +4mv+trell+aic -cmp 2 -subcmp 2 -g 300 -acodec mp3 -ab 128 -ac 2 \
    -async 1 "$OUTFILE"

rm -f pass1.avi ffmpeg2pass-0.log

# vim:set ai et sw=4 ts=4 tw=80:
Maxwell Smart is offline View Maxwell Smart's Photo Album   Reply With Quote
Old 06-07-2007, 14:22   #5
evgesha2jz-ge
Member
 
Join Date: Mar 2006
Posts: 35
Default

hey guys i'm getting this:
C:\1\VideoSynchsoft\ffmpeg.exe ffencode test.avi
FFmpeg version SVN-r9133, Copyright (c) 2000-2007 Fabrice Bellard, et al.
configuration: --enable-gpl --enable-pp --enable-swscaler --enable-pthreads --
enable-liba52 --enable-avisynth --enable-libamr-nb --enable-libamr-wb --enable-l
ibfaac --enable-libfaad --enable-libgsm --enable-libmp3lame --enable-libnut --en
able-libogg --enable-libtheora --enable-libvorbis --enable-x264 --enable-xvid --
cpu=i686 --enable-memalign-hack --extra-ldflags=-static --enable-dirac
libavutil version: 49.4.0
libavcodec version: 51.40.4
libavformat version: 51.12.1
built on May 27 2007 19:45:44, gcc: 4.2.0
Unable for find a suitable output format for 'ffencode'


what am i doing wrong?
evgesha2jz-ge is offline View evgesha2jz-ge's Photo Album   Reply With Quote
Old 06-07-2007, 15:23   #6
Aesculapius
Junior Member
 
Join Date: Jun 2007
Posts: 18
Default

call it like ffencode.bat test.avi, without the ffmpeg.exe
Aesculapius is offline View Aesculapius's Photo Album   Reply With Quote
Old 06-07-2007, 16:08   #7
evgesha2jz-ge
Member
 
Join Date: Mar 2006
Posts: 35
Default

thank you very much
evgesha2jz-ge is offline View evgesha2jz-ge's Photo Album   Reply With Quote
Old 06-08-2007, 12:02   #8
bkd11
Member
 
bkd11's Avatar
 
Join Date: May 2007
Location: USA
Posts: 173
Default

Does this work with .vob files. I'm not sure if I'm doing something wrong, but my converted file always ends up being a compressed .vob file that doesn't play. I also tried a xvid .avi file containing ac3 audio and ended up with perfect video but no audio. I'm a little green to these "commando" type video converter programs. Any help would be appreciated.
bkd11 is offline View bkd11's Photo Album   Reply With Quote
Old 06-08-2007, 12:43   #9
Maxwell Smart
Member
 
Join Date: May 2007
Posts: 35
Default

Quote:
Originally Posted by bkd11 View Post
Does this work with .vob files. I'm not sure if I'm doing something wrong, but my converted file always ends up being a compressed .vob file that doesn't play. I also tried a xvid .avi file containing ac3 audio and ended up with perfect video but no audio. I'm a little green to these "commando" type video converter programs. Any help would be appreciated.
Yes, it does. Just use the batch file (for Windows) or shell script (for Unix or Linux).

I've converted 9 different titles from 2 different DVDs with ffmpeg, and they work fine.
Maxwell Smart is offline View Maxwell Smart's Photo Album   Reply With Quote
Old 06-08-2007, 13:28   #10
bkd11
Member
 
bkd11's Avatar
 
Join Date: May 2007
Location: USA
Posts: 173
Default

Quote:
Originally Posted by Maxwell Smart View Post
Yes, it does. Just use the batch file (for Windows) or shell script (for Unix or Linux).

I've converted 9 different titles from 2 different DVDs with ffmpeg, and they work fine.
I tried both of the .bat files listed above when I got these results (I have Windows XP). Maybe I will try on a different computer to see if I get the same thing.

Last edited by bkd11; 06-08-2007 at 13:31..
bkd11 is offline View bkd11's Photo Album   Reply With Quote
Old 06-08-2007, 14:31   #11
Dase
Junior Member
 
Join Date: May 2007
Posts: 11
Default

I'm glad this is working for more people than just me. I never know if the fact that something works or doesn't work for me is due to something specific to my computer.

And kudos to those of you who have made much better batch files/scripts. I'm using Aesculapius' batch file for my stuff.
Dase is offline View Dase's Photo Album   Reply With Quote
Old 06-08-2007, 14:35   #12
Dase
Junior Member
 
Join Date: May 2007
Posts: 11
Default The ffmpeg output filename must have a .AVI extension

Quote:
Originally Posted by bkd11 View Post
Does this work with .vob files. I'm not sure if I'm doing something wrong, but my converted file always ends up being a compressed .vob file that doesn't play.
It turns out that ffmpeg uses the output file extension to determine the video container format. For the xvid encoding we're doing, the output file must have an AVI extension on the pass 2 ffmpeg command line.

The batch files above use the source file format extension, so if you transcode anything other than an AVI file you're likely to have video problems.

To add the right output file extension to Aesculapius' batch file, change the %newfile% parameter in pass 2 to:

%newfile%.avi
Dase is offline View Dase's Photo Album   Reply With Quote
Old 06-08-2007, 15:30   #13
bkd11
Member
 
bkd11's Avatar
 
Join Date: May 2007
Location: USA
Posts: 173
Default

Thanks, I will try this when I get home tonight. Still wondering why the avi file I tried had no audio. Could if be due to the ac3 having multiple audio streams? If that's the case, I will most likely have the same problem with most of the DVD vob files too.
bkd11 is offline View bkd11's Photo Album   Reply With Quote
Old 06-08-2007, 21:31   #14
bkd11
Member
 
bkd11's Avatar
 
Join Date: May 2007
Location: USA
Posts: 173
Default

Quote:
Originally Posted by Dase View Post
It turns out that ffmpeg uses the output file extension to determine the video container format. For the xvid encoding we're doing, the output file must have an AVI extension on the pass 2 ffmpeg command line.

The batch files above use the source file format extension, so if you transcode anything other than an AVI file you're likely to have video problems.

To add the right output file extension to Aesculapius' batch file, change the %newfile% parameter in pass 2 to:

%newfile%.avi
It worked!

Funny thing though, when I analyzed it in GSpot, it said audio is 0 kb/s (0/ch, stereo) CBR @ 48000 Hz. That's why I thought there was no audio.

But hey it works, that's all I care about.
bkd11 is offline View bkd11's Photo Album   Reply With Quote
Old 06-08-2007, 23:37   #15
yumms
Member
 
Join Date: Apr 2007
Posts: 139
Default

You can use this ffmpeg gui, its much easier.

http://www.free-codecs.com/download/WinFF.htm
yumms is offline View yumms's Photo Album   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 16:55.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.


Copyright © 2006-2011 CrowdGather |  About iAudiophile |  Advertisers | Investors | Legal | Contact