2009-05-08

桑梓贴吧视频数据的解决方案

  由于种种问题以及磁盘阵列空间有限,桑梓贴吧的阵列趋近饱和,为了解决这个问题最近稍微研究了一下,根据实际的发现,桑梓贴吧的视频文件有一些在网站后台已经被删除,但是在服务器上的文件却没有被删除。这样一来,我就在猜想这种情况恐怕不少。于是,经过一番考虑,想到了一个方法。



这个方法的要点如下:


  1. 从数据库中查出所有桑梓贴吧的视频文件名记录,导出到一个新的表。
  2. 从视频文件目录下导出所有视频文件名列表,再倒入到一个新的数据库表中。
  3. 对比两个数据库的文件名字段,筛选出不是同时存在于两个数据库之中的数据
  4. 导出多出的文件列表,删除文件。

那么一步一步看。

  第一步:导出桑梓贴吧的视频文件名记录。假设表是address 字段是link那么。我们直接复制整个表到别的数据库。

  第二步:从视频目录下导出所有视频文件名的列表,只需要cd到视频目录然后:
find ./ -exec ls {} \; &;gt /home/temp/filelist.txt

  这样就成功的吧文件列表导出来了。之后再想办法把这个列表导入到一个数据库表中。

  第三步:假设我们导入的数据表是exist那么现在对比找出在两个表中不同时存在的数据:

insert into temp(link)
select link from fileexist
where not exists ( select link from addresswhere address.link = fileexist.link) ;

  这一步会在一个临时表temp中的link字段中插入在两个表中不同时存在的数据,因此再把temp表的字段导出就成为了我们要挑选出的文件了。

  第四步:要删除文件,由于数量众多需要使用xargs命令:为免意外我先移动到其他目录,再行删除:

xargs -i mv {}&;lt /home/ftp/temp.txt ../tempuse

这个命令就是用xargs把文件名传递到{}的位置,移动过去而已。而且不怕列表里有文件不存在。

好了,一下子删了200多G的东西,世界清静了。。。。

2009-05-05

php扩展的动态编译

有时候需要安装php的模块,这个时候重新编译php就相当不合适了。因此使用类似于apache的动态编译的特性就不错了。对于php来说,我们有很棒的工具,以pdo_mysql为例。
一下假设我的php已经安装在/opt/php
php的源代码在/home/test/php里。
cd /home/test/php/ext/pdo_mysql
/opt/php/bin/phpize
./configure --with-php-config=/opt/php/bin/php-config --with-pdo-mysql=/opt/mysql/

这样这个模块就编译好了。吧php.ini里面对应的模块打开即可。

另外,注意安装pdo需要aclocal这个在autoconf包里的程序。

2009-05-01

JavaFX GUI Learning: Task Learning

Learning from practice!




This article is the note of my learning of JavaFX under the offical javaFX website tutorial:
http://www.javafx.com/docs/tutorials/mediabrowser/index.jsp

Task 1: Loading and Displaying an Image


Graph Programming Model :

  • Stage is the top level container for JavaFX program.
  • Scene is a drawing surface for graphical content and a container that holds the scene graph nodes. It can be added to Stage, and JavaFX renders everything on a scene.
  • CustomNode is a subclass of javafx.scene.Node and created by overriding the create function. It can have one child node.
  • Group is a sequence of child nodes.

  • Node is an element in a scene graph, and javafx.scene.Node is an abstract class. The following visual objects are examples of javafx.scene.Node implementation: javafx.scene.image.ImageView, javafx.scene.media.Mediaview, javafx.ext.swing.*, javafx.scene.shape.*, and javafx.scene.text.Text. These are leaf nodes, which cannot have a child element.

By the tutorials of SUN, it supply a architecture of this program:




In our program, main.fx is the main class file and the first class to be built, we build a stage and add a scene into it.

You can download the netbeans project file to have a detail looking of this project

http://www.javafx.com/docs/tutorials/mediabrowser/dist/module01-task01_nb.zip

The code of offical demo of task 1 is a little confusing to beginner, So I have change some code and comment so that it is easy for my self to understand, if you need, you can download it from http://ulysess.ys168.com/


After the task 1, we have a simply program that can scale-up a image by user's click.


Task 2 :will create a image wall.


So it is a good habbit to define a action by code like this:

package var fullView : function(metaData : MetaData, placeholder: Image) : Void;

object does not need to know how to do fullView, it let other object which will arrange its to do it.

Task 3: Adding a Expansion effect

Expansion effect will scale up you thumbnail 25% bigger while find your cursor is above it

In order to implementation that, we should make the program know how to center the thumbnail after scaled, and handing the expansion such that it does not make the wall resize.

We just need change thumbnail.fx class, put image into a boundingRect and use imageView to painting the thumbnail, and finally some mouse event, that is what we need.

Offical project can be download in http://www.javafx.com/docs/tutorials/mediabrowser/dist/module01-task03_nb.zip



Task 4:Building a Animation Effect

A program provide scale the image must have the basic function of zoom in and zoom out. We need timeline, key frame and interpolators to implement that functions.

  • Timeline is an object that contains a list of key frames, together with a function to control the animation, which might be to start it, stop it, and repeat it.

  • KeyFrame describes a set of end states of various properties (values) of objects at a certain time instant relative to the start of the animation, together with interpolation specifications to calculate the in-between values relative to the previous frame.
  • KeyValue class defines a target, which is a reference to an object to animate, a value for the object to take on at the KeyFrame time, and an Interpolator.
  • The type of interpolation is defined with a KeyValue so that different interpolation types can be used for each KeyValue in a KeyFrame and at different KeyFrame times. Custom interpolators can be written, but the following simple interpolators are provided for animating numeric values:

  • Interpolator.LINEAR - (The default) uses simple linear interpolation.

  • Interpolator.EASEOUT - Start changing the value slowly after the KeyFrame.

  • Interpolator.EASIN - Slow the rate of change as the KeyFrame is reached.
  • Interpolator.EASEBOTH - Smooth the rate of change through the KeyFrame.

  • A shorthand syntax is provided for defining simple KeyFrames.
    at ( &;lttime&;gt ) {
    &;lttarget&;gt =&;gt &;ltvalue&;gt tween &;ltinterpolator&;gt;
    }

    Well, does it looks a little familar? Yes, if you have a basic knowledge about FLASH, It just about the same concept. After you know the knowledge It's a easy task to do this.

    Let us look at the architecture:

    There are two new operator in the code, the => operator provides a literal constructor for a list of key values. The tween operator is a literal constructor for an interpolated value.

    One thing should note in this section is that the interpolator can't be used at will other than it can result your animation play in unwanted way!

    Task 5: Scroll Controller Feature

    We have lots of photos and obviously it can not displayed in one screen, so we need scroll contorller to navigate to others thumbnails.


    In this task we need a new class ScrollControl and add two function in wall.fx

    JavaFX Script Learning: Basic Knowledge

    Declaring Variables:
    There are two type variables in JavaFX, var or def, we can use 'var' and 'def' keyword to declare varibles. the difference between the two is that var may be assigned new value while def not, so def is a little like final variables in Java.
    In JavaFX we can do not explicitly specify variables as its date type, compiler can figure out what is the data types you need, this called type inference.

    Defining and Invoking Functions:
    a basic format of function define:
    function add() { }
    a function with arguments:
    function add(argOne: Integer, argTwo: Integer) {}
    a function with returnvalue of Integer:
    function add() : Integer { var resule = 11; return resule;}
    Note that if no return value is specified, a function returns Void by default.
    Accessing Command-Line Arguments:
    function run(args : String[]) {}
    Note that run function is special that serves as the script's main entry point.
    Invoke a function just like invoke a method in Java.

    Using Object:
    def myAddress = Address { street: "I don't know"; }
    this line code create a Object of Address and assign the value of street instance variable of Address.

    Data Types:
    In JavaFX there are 6 build-in data types: String, Number and Integer, Boolean, Duration, Void, Null.
    String can be declared by single or double quotes.
    Number is floating-point number while Integer is integer number.
    Duriation type represents a fixed unit of time, it should be notated with time literals for example:
    5ms //5 milliseconds
    5s //5 seconds
    5m //5 minutes
    5h //5 hour


    Void is used toe indicate that a function does not return any value.
    null i a special value used to indicate a missing normal value, null is not the same as zero or empty string. you can use 'null' keyword to comparing if its null, in a word, its like null in java.


    Sequences in JavaFX:
    Sequences should be declared in square brackets[] ,such as


    def weekDays: String[] = ["mon","tue","wed","thu","fri"];
    def days : [weekDays, ["sat","sun"]];
    def nums= [1..10];
    //this declared a sequence contains numbers from 1 to 10 with step 1
    def numg= [10..1 step -1];
    //this declared a sequence contains number from 10 to 1 with step -1
    //Note that if create a descending range the second value should less
    // than first
    def nums2 = nums[ n | n &;lt 5]
    //this will create a sequence with the items from mums where value
    //less than 5
    def size =sizeof numg;
    // this will get the size of numg
    def weekend = days[5..6];
    //this will create a sequence with the items of days 6 to 7
    def weekend2 = days[5..];
    //this will create a sequence with the items of days 6 to the end
    //of days
    def weekdays2 = days[0..&;gt5];
    //this will create a sequence with the items of days 1 to 5
    def days2 = days[0..&;gt];
    //this will create a sequence with all items from days,
    //but the last item of days will be excluded


    Operators in JavaFX:
    +-*/is same to Java
    mod get remainder
    not is inverts the balue of boolean
    and or is same to Java
    instanceof is same to Java


    Expressions in JavaFX:
    A block expressions surrounded by curly braces and must be sparated by semicolons. The VALUE of block expression is the value of the last expression. var and def are expressions.
    Look for expression:
    var nums= [1..10] ;
    for(num in nums) { println(nums); }

    2009-04-27

    JavaFX Script Learning: Data Binding and Triggers

    One of the most useful feature in JavaFX is its Data Binding and Triggers functions. According to the official tutorial, Data binding is the ability to create an immediate and direct relationship between tow variables, This allow two or more variables can automatically change when the other or others change. At the same time, A Replace Trigger is a block of code that is attached to a variable, which when the variable changes, the code is automatically executed. So let's begin here.
    Here is an example :


    var x = 0;
    def y = bind x;
    x=1;
    println(y); //y now equals 1
    x = 333;
    println(y); // y now is 333;

    That is a extreme sample example to show the use of 'bind' keyword, You can know in that example, y is binded to x, and its value is changed with x, that's the basic use of bind. Note that y is declared as a def because we don't want any thing change y by assigning a value to it.
    An other example:

    var myStreet= " No.2 Phoenix Garden"
    var myCity = "Xiamen"

    def address = bind Address {
    street : myStreet;
    city : myCity;
    };

    println({address.street});
    myStreet = "No.33 NoWhere"
    println({address.street});


    In this example the result of execute is :
    No.2 Phoenix Garden
    No.33 NoWhere

    So, the conclusion is we can bind a variable with the value of a bound expression, which can be a basic type, an object, the outcome of a function, or the outcome of an expression. But in this example we must note that the changing of myStreet actually causes a new Address object to be created and then re-assigned to the address variable. If you don't want this happen, you can change the definition like this:

    def address = bind Address{
    street: bind myStreet;
    city : myCity;
    }

    By this, you can change street without create a new object, and the bind in def line can be omited.

    In next step, let us look at Binding and Functions.

    Here is an example :

    var scale = 1.0;

    bound function makePoint(xPos : Number, yPos : Nubmer) : Point {
    Point {
    x: xPos * scale;
    y: yPos * scale;
    }
    }

    class Point {
    var x : Number;
    var y : Number;
    }

    var myX = 5.0;
    var myY = 9.0;
    def pt = bind makePoint(myX, myY);
    println(pt.x);

    myX *=2;
    println(pt.x);

    scale = 2.0;
    println(pt.x);

    At the head of this code, we announce a bound function by 'bound' keyword, and then we use our own myX and myY number to create a Point by that bound function, note we use bind keyword before the invocation of makePoint function, so pt binded to the outcome of makePoint function.
    The result of executing is :
     
    5.0
    10.0
    20.0

    But if we remove the bound keyword from makePoint function(it became a non-bound function), the result would be:

    5.0
    10.0
    10.0

    The different between these two situation is a bound function will get re-invoked when every variable related to the function changed while the non-bound function only do that when one of their arguments change. And we MUST NOTE that bound function and bind keyword is work together.


    In next step, let us look at Binding with Sequences:

    Here is an example:

    var q1 = [1..10];
    def q2 = bind for (item in q1) item *8;
    printSeqs();
    insert 11 into q1;

    function printSeqs() {
    println("First Sequence:");
    for( i in q1) { println(i) ; }
    println("Second Sequence:");
    for( i in q2) { println(i) ; }
    }

    You can guessed the result is first sequence is 1 to 10 with step of 1 and the second sequence is 8 to 80 with step of 8.
    In this example, we bind two sequence by placing the bind before the for. For more understanding, we can try to change one or some items of q1, you can find the corresponding items q2 will automatically changed and others will not be affect, like we do in the example we insert a item 11 into the end of q1 and q2 automatically generated a item 88 at the end of itself.

    Finally, let us look at the Replace Triggers.
    It is arbitrary blocks of code that attach to variables and execute whenever the variable's value changes. Here is an example:

    var test = "test" on replace oldValue {
    println(" value has changed, old value is { oldValue}");
    }

    test = "no";

    The trigger will fires two times, first is the initialized to "test" and the next is fires When the code "test= "no"; " executed, the program will report value has changed. NOTE that the oldValue store the value of test before the trigger was invoked.

    2009-04-25

    Ubuntu 快速配置

      新装了9.04,很多东西需要重新配置,未免麻烦,就备份一下这个快速配置的过程了:
    已经安装完系统了,配好了网络.
    首先是添加firefox的一些插件好下东西.
    sudo apt-get install compizconfig-settings-manager
    安装桌面配置的程序.我喜欢配置各个桌面的快捷键。

    在窗口管理器添加右键在当前路径打开终端。
    sudo apt-get install nautilus-open-terminal

    在窗口管理器以root权限打开当前文件夹:
    sudo apt-get install nautilus-gksu

    安装基本编译环境:
    sudo apt-get install build-essential

    接下来安装ATI驱动:
    不知道怎么搞的,新的催化剂不能直接gui安装,在我这里得用
    sudo sh ati-driver-installer-9-3-x86.x86_64.run --buildpkg Ubuntu/9.04
    生成一大堆包以后再安装。
    安装需要的c++库和dkms
    sudo apt-get install libstdc++5
    之后安装所有生成的包:
    sudo dpkg -i *.deb
    重启到recovery模式,root login后
    aticonfig --initial
    ----------------------------------
    OK~~原来还以为可以,结果浪费了我半天时间,事实证明这个在我的X1250下不能用,官方驱动要9.4以后才支持这个版本的xorg,而正好ati在9.4以后不支持HD之外的显卡了,真漂亮。。。9.3装好的成果就是,libglx.so和fglrx_drv.so一个函数名有字符错误,另外一个就是和xorg client的版本不匹配..
    而那个受限驱动,装好以后画屏。。
    没办法,只能忍受该死的不断黑屏把.以后一定要买N卡!!!!!!!!!!!鄙视一下ATI


    接下来编译安装多媒体支持:
    参考以前些的两篇帖子:
    http://rhingheart.blogspot.com/2009/03/mencoderffmpeg.html
    http://rhingheart.blogspot.com/2009/03/flvh264.html

    一个ogg的下载地址这里:
    http://www.xiph.org/downloads/

    另外,还要smplayer可以从这里入手:
    http://smplayer.sourceforge.net/downloads.php?tr_lang=en

    之后jdk mldonkey等用apt解决吧。
    java中文变方块参考这个解决:
    cd /usr/lib/jvm/java-1.5.0-sun-1.5.0.15/jre/lib/fonts
    sudo mkdir fallback
    cd fallback
    sudo cp /usr/share/fonts/truetype/arphic/uming.ttc /usr/lib/jvm/java-1.5.0-sun-1.5.0.15/jre/lib/fonts/fallback

    Ubuntu 9.04 国内教育网的源

    从我这里看,还是中科大的源品质最好,没有出现404,交大有几个页面是404,所以就选择中科大了:

    科大:
    ----------------------------------------
    deb http://debian.ustc.edu.cn/ubuntu/ jaunty main restricted universe multiverse
    deb http://debian.ustc.edu.cn/ubuntu/ jaunty-backports restricted universe multiverse
    deb http://debian.ustc.edu.cn/ubuntu/ jaunty-proposed main restricted universe multiverse
    deb http://debian.ustc.edu.cn/ubuntu/ jaunty-security main restricted universe multiverse
    deb http://debian.ustc.edu.cn/ubuntu/ jaunty-updates main restricted universe multiverse
    deb-src http://debian.ustc.edu.cn/ubuntu/ jaunty main restricted universe multiverse
    deb-src http://debian.ustc.edu.cn/ubuntu/ jaunty-backports main restricted universe multiverse
    deb-src http://debian.ustc.edu.cn/ubuntu/ jaunty-proposed main restricted universe multiverse
    deb-src http://debian.ustc.edu.cn/ubuntu/ jaunty-security main restricted universe multiverse
    deb-src http://debian.ustc.edu.cn/ubuntu/ jaunty-updates main restricted universe multiverse
    -----------------------------------------

    上交大:
    -----------------------------------------
    deb http://ftp.sjtu.edu.cn/ubuntu/ jaunty main multiverse restricted universe
    deb http://ftp.sjtu.edu.cn/ubuntu/ jaunty-backports main multiverse restricted universe
    deb http://ftp.sjtu.edu.cn/ubuntu/ jaunty-proposed main multiverse restricted universe
    deb http://ftp.sjtu.edu.cn/ubuntu/ jaunty-security main multiverse restricted universe
    deb http://ftp.sjtu.edu.cn/ubuntu/ jaunty-updates main multiverse restricted universe
    deb http://ftp.sjtu.edu.cn/ubuntu-cn/ jaunty main multiverse restricted universe
    deb-src http://ftp.sjtu.edu.cn/ubuntu/ jaunty main multiverse restricted universe
    deb-src http://ftp.sjtu.edu.cn/ubuntu/ jaunty-backports main multiverse restricted universe
    deb-src http://ftp.sjtu.edu.cn/ubuntu/ jaunty-proposed main multiverse restricted universe
    deb-src http://ftp.sjtu.edu.cn/ubuntu/ jaunty-security main multiverse restricted universe
    deb-src http://ftp.sjtu.edu.cn/ubuntu/ jaunty-updates main multiverse restricted universe
    --------------------------------------

    ps.ubuntu 9.04的新主题相当不错,启动速度也相当牛B,在我机器上只需要19秒左右就进系统了。。。

    2009-03-31

    过程生成与无限星辰这个游戏

      最近无限星辰(Infinity)这个游戏又放出了新的截图,用GPU来进行它的过程生成运算,使得行星的表面有一次有了质的飞跃。
      我们先看看这个游戏的自我介绍:

      Infinity: The Quest for Earth(译名:《无限星辰:失落的地球》)是一个基于未来宇宙背景的大型多人在线游戏,目前还在紧张开发中。

      这款游戏采用了一种先进的过程生成引擎。它会使玩家在宇宙中的探索看似无限大,而其中拥有着一颗颗独立的星球,同时在每颗星球上都可以做到无缝着陆。游戏将会给新老玩家提供一个宏大的故事背景,在宇宙中错综复杂的政治下,充斥着各种社会集团、公司和其他组织,而主线,将围绕游戏中的三大势力——人马座集团、德尔塔联邦和斯塔弗同盟之间的斗争展开。

    通过使用I-Novae引擎先进的过程生成技术,《无限星辰》已经打破了往日游戏在固定空间内探索束缚。它将使动态生成数不清的星系与星球成为可能,每个独一无二的角落都在等待着发现它的人。通过绘制合理的天文距离与比例,每个在宇宙中不停运动天体轨道都会被精确地计算出来。在这里,从一睹遥远的双星系星球上两个太阳的日出,到观看一个距离你所在家园外千万光年的小月亮的月全食,都将成为可能。每一组景色,每一条地平线,在《无限星辰》世界里,都完全会是独一无二的。


      怎么样?听起来不错吧?网游就应该是这样个样子才对。
    它的主页:http://www.infinity-game.com/infinity/index.php


      那么,这篇文章的主要目的还是通过这个游戏引入一种概念的介绍:Procedural Generation,这里被翻译作过程生成,其实理解就是在实际游戏的过程中生成游戏中相应的对象,这个游戏号称拥有的数十亿棵星球,每颗都有着有很多细节的地表。而这每颗星球的详细内容都是玩家在自己的机器上开启游戏后生成的。怎么样?神奇吧?
      其实,要理解这个过程生成技术必须要从它的数学原理下手,那就是分形,这里有一篇介绍这一思想的文章:递归之美:数学,电脑科学与分形 ,转自:http://mmdays.wordpress.com/2007/05/24/recursive/




    monalisa-recursion想像一下,我剛才說了一句話,那句話是:「想像一下,我剛才說了一句話,那句話是:「想像一下,我剛才說了一句話,那句話是:……….」」,如此下去,就好像站在兩面平行擺設的鏡子中間,鏡子中的影像不斷的重複。再舉個例子,寫完一封信想要匿名保密,就署名「知名不具」。回信的人寫:「知 知名不具 具」。之後再回信的時候就變成:知知知名不具具具,加上括號可能比較清楚:(知(知(知名不具)具)具)。


    遞迴就是類似這樣子,不斷的重複同樣的東西,只不過每次重複的是比較小的東西了。大家應該對數學歸納法不陌生,在使用數學歸納法時,我們首先確定 n=1 的時候某件事情是成立,然後在證明 n 到 n+1 的過程是正確的,就可以從 n=1 的例子,一路推論出第 n 項是甚麼東西。就像是推骨牌一樣,把第一張牌推倒了之後,剩下的骨牌自然就被前面的骨牌給推倒。



    遞迴的概念則是相反的方向:我們想要解決一個大小為 n 的問題,我首先做的事情是把問題化簡成大小為 n-1 的問題,但是解決的方法還是一樣,只不過大小是 n-1。如此繼續化簡,最後變成大小為 n=1 的基本問題,接著只要n=1的基本問題解決了,原來大小為n的問題也跟著解決了。


    這又好像層層分工。假設每個人都會加法,然後今天我想求出 1+2+…+n 等於多少?其中一個辦法就是遞迴,我先假設 1+2+…+(n-1) 已經有人算好,那麼我只要再加上 n,就可以得到答案了。然而 1+2+…+(n-1) 要怎麼得到呢?我就請另外一位朋友幫我算。另外一位朋友接到這個問題以後,也用同樣的方法,他把 1+2+…+(n-2) 的結果交給另外一位朋友算,然後把這個結果加上 (n-1),就變成我想要的 1+2+…+(n-1) 了。朋友的朋友也繼續用類似的方法,直到最後一位朋友只需要回答1,接著倒數第二位朋友就把1加上2,傳給倒數第三位朋友,倒數第三位朋友加上3,一直到我收到 1+2+…+(n-1) 的結果,再加上 n,就大功告成了。


    recursion


    不過可能會覺得,如此簡單的問題,為何還需要遞迴呢?其實遞迴也是比較適合一些問題來解,也就是那些「解決方式一樣,但是可以化成大小比較小」的問題,除此之外還可以輕鬆解決基本問題(n=1的時候)。舉例來說,有個古老的問題叫做河內塔 (Hanoi Tower),問題的定義引述如下 (引述網站)


    haoi-tower1883年,一位法國的數學家 Edouard Lucas 教授在歐洲的一份雜誌上介紹了一個相當吸引人的難題──迷人的智力遊戲。這個遊戲名為河內塔 (Tower of Hanoi),它源自古印度神廟中的一段故事 (也有一說是 Lucas 教授為增加此遊戲之神秘色彩而捏造的)。傳說在古老的印度,有一座神廟,據說它是宇宙的中心。在廟宇中放置了一塊上面插有三根長木釘的木板,在其中的一根木釘上,從上至下被放置了64片直徑由小至大的圓環形金屬片。古印度教的天神指示祂的僧侶們將64片的金屬片移至三根木釘中的其中一根上。規定在每次的移動中,只能搬移一片金屬片,並且在過程中必須保持金屬片由上至下是直徑由小至大的次序,也就是說不論在那一根木釘上,圓環形的金屬片都是直徑較小的被放在上層。直到有一天,僧侶們能將64片的金屬片依規則從指定的木釘上全部移動至另一根木釘上,那麼,世界末日即隨之來到,世間的一切終將被毀滅,萬物都將至極樂世界。


    倘若這個故事的敘述為真,那麼,我們只需加速移動金屬片,是不是就能愈早到達極樂世界呢?果真要移動這64片金屬片,那麼,至少要花幾次的搬動才能完成呢?有沒有規律可循呢?


    這個問題,就很符合剛才的特性:我們可以把大問題化成小問題,而且解決的方法相同,只不過問題的大小變小了。另外基本問題(n=1),就是移動一根金屬片所需要的次數,這個我們也可以輕易解決,所以這個問題就可以用遞迴來解。


    首先,我們假設有A、B、C三根柱子,這64片金屬片一開始在柱子A上面,我們想要搬到柱子C。因為問題中規定某個金屬片上面是空的時候才能移動,我們就假設有個人可以幫我們把63片比較小的金屬片先從柱子A搬到柱子B上面,然後我們把最大的那一片從柱子A搬到柱子C,再請那位朋友把剛才的63片從柱子B搬到柱子C,整個問題就解決了。然後我們只要知道剛才那位朋友搬了幾次,然後加上我們自己般動的1次,就是整個問題要求的搬動次數了。


    遞迴不僅僅在數學上有其重要性,在電腦科學之中扮演的角色更是至關重要。程式設計者對於遞迴絕對不會陌生,上面所舉的河內塔問題,實際上也是電腦科學的經典例子之一,是初學程式設計的人一定會學到的東西。遞迴的思維,常常可以讓程式設計者打造出簡潔的程式,讓繁冗的問題透過簡單的程式碼來解決 (例如 parser 的設計)。演算法上所講的 dynamic programming,就是遞迴思維在演算法的具體呈現。


    fractal-broccoli遞迴同時也是碎形 (fractal) 這門大學問的基石,碎形是一種相當美妙的幾何圖案,就如同上面那一張蒙娜麗莎的圖一樣,圖中有圖,形中有形,且小的部分都是大的部分的縮影,我們就稱之為碎形。碎形本身的數學定義,實際上就包含了遞迴定義在裡面,我們甚至於可以說,碎形是遞迴在幾何學的一種具體呈現。但是碎形不僅僅是一種數學概念而已,在自然界中,有許許多多的地方都出現自然的碎形,讓人讚嘆遞迴原來就出現在我們的生活周圍。圖中的這棵花椰菜,就蘊含了遞迴的碎形圖案與於其中。碎形同時也在各個研究領域有著廣泛的應用,光是在電腦科學領域,就有人把碎形應用在影像和影片壓縮之上 (這不難想像,由於碎形這種以小見大的特性,我們可以用小的來表現大的,因此可以有壓縮的概念出現),在電腦圖學上 (computer graphics),也有人把碎形應用在設計電腦遊戲之中的一些景物,打造出有效率和簡潔的系統。現在電腦遊戲之中的景物,很多都是玩家邊玩、遊戲系統邊產生出即時的景物,這叫做 procedural generation,這種即時產生景物的技術,可以避免遊戲軟體預先儲存一堆要展現的景物,幫整個軟體瘦身。procedural generation 就使用了大量的碎形產生與合成技術於其中,而這些都根植於遞迴這一個深刻卻簡單的思維。


    至於把碎形應用在遊戲之中,現在已經做到有多可怕的地步了呢?請大家看看以下的三張圖片,不妨猜猜擁有這種精緻畫面的遊戲軟體,其整個遊戲的size大小是多少呢?


    kkrieger-screenshot2 kkrieger-screenshot3 kkrieger-screenshot1


    正確答案是97KB!沒錯,我沒有打錯字,你的眼睛也沒有看錯,這款遊戲的大小只有 97KB!傳統的一片 3.5 吋磁片可以裝下十幾個這款遊戲!這一款第一人稱的射擊遊戲叫做 .kkrieger,是由德國的 demogroup .theprodukkt 所開發,截至目前為止還在beta測試版的階段,這款遊戲之所以可以壓縮到這麼小的境界,就是因為遊戲之中的場景和音樂幾乎全部都是由動態產生,遊戲之中預先存放的資料只有一些簡單的幾何形狀和 MIDI 音樂檔,所以自然檔案大小非常小。如果這款遊戲沒有用 procedural generation 的技巧進來的話,估計檔案大小會爆增成 200~300MB,這樣的技術,真是令人嘆為觀止。而背後最大的功臣,就是這篇文章談到的遞迴和碎形。各位也不妨下載來玩玩看吧 (下載點)。不過需要注意到一件事情,這款遊戲的載入時間非常長,因為他要靠著一點點的程式碼即時來運算製造出場景,所以要耗去很多計算時間,這可說是一種 time 和 space 的 tradeoff。


    看完這篇文章,各位有沒有對看似枯燥的數學有了一點點不同的看法呢?沒想到遞迴可以這樣應用在遊戲開發之中吧。下次學習數學感覺到枯燥時,不妨從應用的角度切入試試看吧!




    过程生成简述
      过程生成的定义可以在维基上找到:

      http://en.wikipedia.org/wiki/Procedural_generation
      

      但是呢,不要把过程生成仅仅看作游戏编程里的概念。 过程生成的理念具有深远意义,比如它可以取代人类来自动的处理一些重复性的智能工作。 更加有趣的情况是,通过将自动性与一系列过程代码的集成,人们完全可以做到曾经不可想象的事情。比如最近无敌的作品《孢子》http://en.wikipedia.org/wiki/Spore_%28game%29

    一下是infinity论坛的一群人关于此群聊的内容:

    Freeman:
    看上去“过程生成”在游戏中占很重要的地位!怎么去理解它?有没有专业解答?诚挚谢意!
    Magellanic:
    没有专业解答,我相信之前我给出过基本介绍.
    首先,Infinity项目负责人(IA)将其描述为:
    “过程生成”是一种让电脑实时或按要求的生成宇宙的技术,而不是预先制作好。因此,创造出的宇宙理所当然很大很大。数以万亿的世界,等待着冒险者们的探索,然而大多数可能永远不会被发现。
    我理解为,这是一个电脑程序,它会用数学算法创造一个多变的宇宙。我相信这多变性叫做“seeds”。(译:seed?gundan
    seed?我就不翻了)不同的输入会得出不同的结果。如果你十分不熟悉数学,那我就用一个简单的代数方程来解释。
    5 + x = y
    如果 x = 1,
    那么 y = 6 如果 x = 5, 那么 y= 10
    对于每一个x值,都对应着一个不同的y值,由此会得出许许多多结果。我是在ICP测试中知道的。用这个理念,我们可以创造各不相同的世界,因为我们输入了不同的数据。
    而且,因为公式是不变的,只要输入相同的“seed”,就会得出相同的结果。所以,宇宙的物理参数不需要存储在你的电脑里,系统保留其特有的属性,都依赖于“seed”。
    但我不是个程序员。只是个普通的玩家。如果你要更多的信息,等开IA回答吧,或用google,原谅我可能给出的错误信息。


    Delver:
    Hi
    Freeman.我对数学不是很在行。这很伤脑筋。
    我想你肯定会中文。(译:haha,被看穿了)
    我不能用英语简单的叙述,我试试吧。
    一个程序是一个可以用来做任何事的数学公式。像是画一个圆或三角。它同样可以用来解决复杂的问题。作出树,行星或更复杂的东西。你可以让程序记住东西将会是什么样子。现在如果你有真正复杂的事,你就会想这一定很难。用数学就可以使其更简单。电脑要做很多工作,但它确实比人工制作星球,城市简单的多。
    我可以用很长时间用图画软件Photoshop或3dstudio去画一张看上去很像地球图。者要很长时间。
    除了做这些,如果确实太复杂,我可以用一系列的程序来创建一个球体,然后填充如岛屿,水,空气...我怎么做呢?可以给每一样物品编上号。我可以把红色描述为数字FF0000(16进制),我可以将树描述为数字,涂上绿色0000FF。
    我如果正确使用这些数字画出一个星球,我就可以做出一个同样的。如果我记得画这个星球的数字,我就可以告诉你,你也可以做出一个相同的。
    简而言之:
    程序是一个储存或创建数字来创造东西的数学公式或告诉你这东西是什么。你可以想像成制作东西的便宜法门。如果数字相同,公式相同,我们创造的东西也永远相同。如果我改变一点,创造出来的东西就完全不同了。

    Freeman:
    感谢你们Magellanic &
    Delver。感谢你们现身说法。

    DeAdLy_cOoKiE:
    我觉得“过程生成”是在一个大空间下,所有事物都是由程序产生的.许多老套的(译:应该是old school,我讨厌学习不认真的)演示都用“过程生成”创造物体来炫耀,因为(cause,再次bs)电脑的内存在当时十分有限。当然也有用它做游戏的(特别是地图),midwinter, liberation等等。你可以通过变更参数来得出完美的效果。

    Delver:
    freeman,你最好再多找点资料。看看fractals(分形),这是最早的“过程生成”的图形演示。这超过我的能力了。
    除了这些问题,有时我觉得这技术在未来会越来越重要。我的假设在我上个帖子写过了。什么会被刻画?什么会被创造?想象一下整个宇宙都可以随机产生!

    terra0nova:
    呃,“过程生成”不会储存任何数据,这正是其美丽之处,数据可能会放在内存里,但不会在硬盘上(当然要在内存里暂存,但产生出来的行星数据,不会储存,程序会决定行星的位置)给出坐标,程序(关于坐标的算法,像是伪随机算法,把坐标作为seed[译:呵呵])就会产生出你需要的行星。行星信息可能会暂存在内存中,以防你再次来(节省时间),但你一关游戏,数据就被删除了,如果需要,将再次由程序产生数据。
    最大的问题是算法的速度,如果我们要这游戏与现实时间同步(很可能你到之前,行星就已经产生好了),即使如此,这算法仍要更快更完美(可以先大致产生些数据[星球外廓],然后再慢慢填充细节[城市和地面细节])。我不知道这些算法的速度,但如果做得太花,速度就会慢下来。城市看上去很有趣,但我们想要更大,更科幻的。

    CutterJohn:
    想象一下:
    假如这就是我们的过程引擎:

    X = 2y
    那么现在我就向在座各位解释一下我的这个微型过程引擎。
    请看,如果让Y = 5 (也就是所谓的‘seed’)。
    每个人都拿出计算器来算一下X是多少。都是10吧? 如果不是,去买个新的。。。
    很明显,我的“过程”引擎是很简陋的,但依然是一个不错的类比。
    其实所谓的“过程”就是一堆用来计算所需答案的神奇方程式组。而游戏里的每一个客户端都内嵌了相同的方程式组。那么在给定同一个“seed”的情况下,这组方程式(完全一致的,记住)将在每一台电脑上输出一模一样的结果。这就好比你们计算如上所示的那个微型引擎必然得出相同结果一样。


    本段内容出自http://www.infinity-game.com/bbs/viewthread.php?tid=491

    2009-03-28

    MySQL基本使用

      虽然没爱,但是会用到的,所以呢,还是学习一下。这里已经表示你用mysql命令登录进了数据库的情况了。

    命令
    mysql> SELECT VERSION(), CURRENT_DATE;
    这个基本可以说明问题了,mysql的命令都是以;结尾的,当然注意是半角的,而且不区分大小写。
    而显示都是以这种形式出现的:

    +---------------+--------------+
    version() current_date
    +---------------+--------------+
    5.1.29-rc-log 2009-03-28
    +---------------+--------------+
    1 row in set (0.00 sec)


    这里举例:

    mysql> show databases;
    +--------------------+
    Database
    +--------------------+
    information_schema
    joinus
    mysql
    subversiondatabase
    test
    +--------------------+
    5 rows in set (0.00 sec)

    这个命令显示了你的机器上有哪些数据库

    mysql> use test;
    Database changed

    这个命令选择了test这个数据库

    显然,作为一个提供数据存储服务的程序,我们需要分配用户和对应的数据库,这里我们新建一个用户以允许用户登录到mysql,并且控制它自己的数据库。
    所以这个命令等同于新建用户的:

    GRANT priv_type ON db_name.table_name TO user_name IDENTIFIED BY ‘password’[WITH GRANT OPTION]





    其中,大写的单词都是语法中的关键字,小写的部分是需要用户来完成,其中:

    priv_type 表示权限类型,比如 select 、 insert 、 create 等,详细的权限名称可以参考 user 表中 field 字段名称,去掉后缀“ _priv ”后就是实际的权限名称。如果要授予全部的权限,可以用“ all privileges ”来代替。

    • db_name 表示数据库名称,‘ * ’表示所有数据库
    • table_name 表示表的名称,‘ * ’表示所有表。
    • user_name 表示要创建的用户名称,注意这个名称由用户名和 host 两部分组成。
    • password 表示用户的密码,用明文表示,创建的时候会自动进行加密。
    • WITH GRANT OPTION 表示新用户可以将自己的权限授予其他用户,如果不写,默认不可以。

    这里需要注意的是,如果单独赋予管理权限,则“ ON db_name.table_name ”字句中的“ db_name.table_name ”需要写为“ *.*” ,因为管理权限是和某个具体的数据库无关的。

    下面,我们创建三个用户,一个是超级用户,授予它对所有数据库的所有表的所有权限;另外一个是普通用户,授予它对数据库 test1 中表 t1 的 select 、 update 、 insert 、 delete 权限,这种情况适用于大多数情况,因为我们出于对数据的安全性考虑,一般不会给用户设置为超级权限;第三个是需要执行文件操作的用户,需要授予 file 权限。

    创建超级用户:

    mysql> grant all privileges on *.* to user1@localhost identified by '123';

    Query OK, 0 rows affected (0.04 sec)

    创建普通用户:

    mysql> grant select,update,insert,delete on test1.t1 to user2@localhost identified by '123';

    Query OK, 0 rows affected (0.01 sec)

    创建具有文件权限的用户:

    mysql> grant file on *.* to user3@localhost identified by '123';

    Query OK, 0 rows affected (0.01 sec)

    上例中的用户 host 都设置为 localhost ,只允许本地连接,这个值被记录到 user 表中的 host 字段中, host 可以是以下设置:

    A. 主机名或 IP 号,或“ localhost ”指出本地主机。

    B. 使用通配符字符 “%” 和 “_” 。其中,“ % ”匹配任何主机名, 使 用 “_” 匹配任何单个字符。空 Host 值等价于“ % ”。它的含义与 LIKE 操作符的模式匹配操作相同。例如,“ % ”的 Host 值与所有主机名匹配,而“ %.mysql.com ”匹配 mysql.com 域的所有主机。



    这里我新建了一个用户:

    grant all privileges on test.* to testuser@localhost identified by '123456';

    当我使用这个用户登上去并使用

    mysql> show databases;
    +--------------------+
    Database
    +--------------------+
    information_schema
    test
    +--------------------+
    2 rows in set (0.00 sec)

    所以我只能看到我有权限的数据库。
    不过这里有个有趣的地方是,只要管理员给用户指定一个数据库的权限,如果这个数据库不存在的话,用户登录上来后可以自己创建。
    管理员分配了ireport数据库的权限:
    grant all on ireport.* to 'ulysess'@'localhost'
    比如我用testuser登录后
    mysql> create database ireport;
    Query OK, 1 row affected (0.00 sec)


    关于用户权限,在mysql数据库中,是有一个叫做权限表的东西的,这个可以读这篇引用:



    权限系统认证过程实际是通过 MySQL 内部的 6 个权限表来实现的,分别是: user 、 db 、 host 、 tables_priv 、 columns_priv 和 procs_priv ,它们都位于 MySQL 安装后的“ mysql ”数据库中,这些权限表的作用如下:

    A. user 控制 “ 用户名 ” ( user )可以从哪一台 “ 主机名 ”(host) 访问 mysql ,该表包括全局权限的控制。例 如,如果 user 表授予用户 DELETE 权限, 那么此用户可以在服务器主机上删除任何数据库中的记录!换句话说, user 表权限是超级用户权限。只把 user 表的权限授予超级用户如服务器或数据库主管是明智的。对其他用户,应该把在 user 表中的权限设成 'N' 并且仅在特定数据库的基础上授权。

    B. Db 规定哪个用户可以访问哪个数据库。权限列决定允许哪个操作。授予的数据库级别的权限适用于数据库和它的表。

    C. Host 表与 db 表结合使用在一个较好层次上控制特定主机对数据库的访问权限。

    D. tables_priv 和 columns_priv 表类似于 db 表,但是权限更加精细:它们在表和列级应用而非在数据库级。授予表级别的权限适用于表和所有它的列。授予列级别的权限只适用于专用列。

    E. procs_priv 规定谁可以执行哪个存储过程。

    其中, user 表最为重要,它控制着整个 MySQL 的全局权限。 user 中的列主要分为四个部分:用户列、权限列、安全列、资源控制列,其中,我们用的最多的是用户列和权限列,权限列又分为普通权限和管理权限。普通权限主要用 于数据表的操作,比如 select_priv 、 create_priv 等;而管理权限主要用来对数据库进行管理的操作,比如 process_priv 、 super_priv 等等,普通权限在其他权限表中都可能有记录,而管理权限只在 user 表中有记录。

    当用户与 MySQL 进行权限认证时,权限表的存取过程如下:

    A. 先从 user 表中的 host 、 user 和 password 三个字段中判断连接的 IP 、用户名和密码是否存在于表中,如果存在,则通过身份验证,否则拒绝连接。

    B. 如果通过身份验证,则通过 user 、 db 、 host 、 tables_priv 、 columns_priv 、 procs_priv 这几个权限表得到对应的数据库权限并进行授权。




    恩,适时锻炼,打完收工,暂时只需要用到这么多,先这样吧。。。

    ------------------------------------------------------------------------------------
    更新:
      我等必然常用到的 Mysql的一个常用功能就是修改用户和密码,那么看看三种修改用户密码的方法:
    • 登录mysql后
      use mysql;
      UPDATE user SET password=PASSWORD("NEW_PASSWORD") WHERE user='USER_TO_BE_CHANGE';

        mysql> FLUSH PRIVILEGES;
    • 登录mysql后
      SET PASSWORD FOR hunte=PASSWORD('NEW_PASSWORD');
    • 在系统的命令行下
      mysqladmin -u root "old password" "new password"


    ----------------------------------------------------------------------------------
    再次更新:
    删除用户:
    DELETE FROM user WHERE User="testuser" and Host="localhost";
    删除数据库:
    drop database testdatabase;

    2009-03-26

    [JavaFX]Introduce

      这个学期的一个项目需要写手机客户端,结果刚好现在机会不错,所以就学下JavaFX。



      首先需要下载开发用的工具,作为程序员我们需要JavaFX SDK,不过这个东西不好用,所以建议直接用Netbeans for JavaFX。另外还有个JavaFX Production Suite,这个东西会作为插件装进Photoshop或Illustrator,你可以直接用那两个东西弄成的图片生成JavaFX要用的东西。



    JavaFX的框架是这样的:




    实际上JavaFX用的是JavaFX Script的脚本。

    从第一个Hello World程序开始了解一下情况:

    使用Netbeans建立的一个JavaFX项目默认会有一个Main.fx的文件。内容是这样的:






    package learning;

    import javafx.stage.Stage;
    import javafx.scene.Scene;
    import javafx.scene.text.Text;
    import javafx.scene.text.Font;

    Stage {
    title: "Application title"
    width: 250
    height: 80
    scene: Scene {
    content: Text {
    font : Font {
    size : 16
    }
    x: 10, y: 30
    content: "Application content"
    }
    }
    }

    所以呢,事实上也就是这么回事。
    说明:
    Stage是最顶部的容器窗口,用来显示所有可视的 JavaFX 对象。默认的实例变量 title、width 和 height 分别定义了出现在窗口顶部边框里的文本及其高度和宽度。场景变量定义了一个 Scene 对象文字(设置 JavaFX 对象所放置的区域)的实例。

    Scene这个对象文字类似于一个表现应用程序图形内容的刻画草图。scene 实例变量包含一个用于保存 JavaFX 图形元素的 content 变量,并定义应用程序的图形内容。
    必须注意的是,当你为content增加元素的时候,比如上面只有一个Text,你要再加其他的Text,这些元素必须要用[]框起来。

    作为HelloWorld程序 sun官方给的教程已经很不错了,可以自己参考:
    http://javafx.com/docs/gettingstarted/javafx/create-first-javafx-app.jsp

    接下来是其他的学习资源,当然,只有英文的了:

    2009-03-25

    传说中的java coding convention on one page


    相当有趣,毋需多言。图片说明一切。

    2009-03-21

    用ireport+jasperreport制作报表

      所谓报表其实就是把数据按照某种方式整合起来,用适当的形式展示出来的东西。所以它的两个基本要素就是数据和表现,前者基本就是在各种实验、调查中获得的啦,后者嘛,jasperreport是一种比较方便的实现的工具,当然还有其他的。
      用jasperreport制作一个report一般有如下四个步骤:

    1. Create a template
    2. Compile the template
    3. Fill report
    4. Export report
    ok,说明一下,第一步,一般用jasperreport的gui程序ireport创建一个template,这个东西其实只是一个xml文件,不过话说回来,开放式文档标准之后,还有什么不能用xml呢?只是要长的漂亮大家才爱用就是。。。
    第二步,编译其实就是把上步的那个xml文件编译成一个二进制文件,这个虽然可以不编译,不过因为模板很常用所以弄一下就一次编译多次使用咯。
    第三步,这个其实应该要在第一步搞定,但是实际当弄好一个模板以后只需要从这一步开始,所以放到这里,如果是第一次制作这个模板,那么有两种方法fill,第一种是在要fill的地方直接写sql语句,第二中是创建一个自己的数据源,不过这种需要extent JRDataSource这个类。
    第四步,看你的需求,可以导出成pdf,html等等很多很多种。


    接下来以我的ireport 3.50来练习一个报表。
    新装的ireport长这样的:

    既然是quick start,也非常简单,我这里直接用Database JDBC Connection
    设置好你的配置,连接测试成功就ok了。
    如果你是一个新数据库的话,你得先在服务器端弄好你的表。
    我的长这样:
    mysql> show tables;
    +-------------------+
    | Tables_in_ireport |
    +-------------------+
    | testreport |
    +-------------------+
    1 row in set (0.00 sec)

    mysql> describe testreport;
    +--------------+-------------+------+-----+---------+-------+
    | Field | Type | Null | Key | Default | Extra |
    +--------------+-------------+------+-----+---------+-------+
    | title | varchar(40) | YES | | NULL | |
    | author | varchar(30) | YES | | NULL | |
    | isbn | varchar(30) | YES | | NULL | |
    | pic | varchar(80) | YES | | NULL | |
    | perchasedate | date | YES | | NULL | |
    +--------------+-------------+------+-----+---------+-------+

    然后用创建新report的quick start。
    选定好以后那个数据库查询(Query)只需要select * from 你的表 即可,这里主要是ireport获得表字段。


    至于之后的group是干什么的呢,一张图就说明问题了,我这里以title 和 perchasedate为两个group的报表效果:

    我这里录入了两条数据,其实就是图书的一些资料。当然这个报表的模板是默认的,相当的丑。

    不过新版本有一个好的地方就是,早期的各种问题,诸如中文字体,和其他很多乱七八糟的问题都已经被解决了。

    Jasperreport + ireport环境的配置

      最近才知道很多人用jasperreport和ireport来做报表,其实前者使用xml定义报表的模板,能够已多种格式输出成各种报表,后者只是前者的一个gui而已(但是ireport现在的版本其实是基于netbeans的),因为懒得玩window的,所以我直接就在我的ubuntu上来构建这个环境了,这篇帖子只说如何部署,所以没有概念定义。
      首先准备好东西:
      ireport:
    https://sourceforge.net/project/showfiles.php?group_id=64348
      ireport也是一个挂在sourceforge下的项目,它的自我介绍是:
    iReport is the most popular visual reporting tool for JasperReports (Java reporting library) and JasperServer (reporting server). You can manage charts, images, subreports, etc. Data sources: JDBC, TableModels, JavaBeans, XML, Hibernate, CSV etc. Output
      注意下载的ireport和jasperreport的版本似乎要求一致,另外貌似还有netbeans的插件版。
      jasperreport是java程序所以直接下载jar包即可
    https://sourceforge.net/project/showfiles.php?group_id=36382&package_id=28579

    下载好ireport之后你直接运行bin目录下的对应你的系统的执行文件。

    最后再吐槽一句:
    其实ireport现在的版本是一个和netbeans有着相当关联的东西,可以当作是netbeans的一个可视化报表设计改进版。
    而jasperreport其实就是它的编译器了。
    注:3月25日,ireport发布了最新的3.50版。

    为你的网站创建一个IE8 Web Slice

      IE8的Web Slice不同于原来的那种栏目标签只显示一个标题在favourite bar那样,它能够简要的显示网站所有者定义的一个web slice的内容,Digital Inspiration的作者自己演示了这个效果,我就直接帖它的截图:
      按照微软自己的定义,web slice可以让用户方便在浏览器的收藏夹栏上查阅自己感兴趣的简短的消息,比如淘宝上的竞拍价格,天气预报,股票价格等等。可以说它相当于一个嵌入在浏览器中的小网页。
      那么,接下来我们就来研究一下如何快速的为你的网站创建一个web slice。我这里用digital inspiration的实例,虽然它是for wordpress的,但是其实本质了解了就一样了:

    MSDN Library已经有很不错的guide文章和个性化定制的文章了

    要创建一个web slice其实是非常简单的:

     <html><head>
    <title>Page Title</title>
    </head><body>
    <div class="hslice" id="techologynews">
    <h2 class="entry-title">IE 8 web slice</h2>
    <div class="entry-content">
    <p><a href="'#'">Link 1</a></p>
    <p><a href="'#'">Link 2</a></p>
    <p><a href="'#'">Link 3</a></p>
    </div></div>
    </body></html>

    IE8 web slice只是一个简单的使用上面这个结构的html页面而已,你只需要指定它的标题和改变entry-content里面的每条项目内容即可。你也可以在这里定义你自己的css.
    一下是一个wordpress的例子:

    创建一个页面,比如webslice.php,把它放在wordpresss的安装路径下,内容:
    <html><head>
    <title>Page Title</title>
    </head><body>
    <div class="hslice" id="1">
    <h2 class="entry-title">Your Site Name</h2>
    <div class="entry-content"><ol>
    <?php
    require_once('wp-load.php');
    $results = $wpdb->get_results("SELECT post_title, guid
    FROM $wpdb->posts where post_status = 'publish'
    ORDER BY ID DESC LIMIT 5");
    foreach($results as $row) {
    echo "<li><a href="'">guid . "'>"
    . $row->post_title ."</a></li> \n";
    }
    ?>
    </ol></div></div>
    </body></html>

    这段代码连接到wordpress的数据库并且创造一个最新的post的列表,这个列表的内容将会被web slice读取。

    2009-03-05

    Skills of write a Scientific Paper

    These post is my note of learning the skills of write a scientific paper.


    The KEY characteristic of Scientific writing is clarity. Different frome general articals, Scientific writing should be as clear and simple as possible, in a word, the flowery literary embellishments which can cause confusion such as the metaphors, the similes, the idiomatic should NOT be used inwriting research papers

    Title:
    A good beginning is helf done, so is a good title to a paper, many people will read the title, therefore, all words in the title should be chosen with great care, a most common error is defective titles, we need a good title.
    A good title use the fewest possible words that adequately describe the contents of the paper.
    About the length:
        A title too short always do not helpful to potential reader, but a title too long often less meaningful than short ones. A good title should not contain "waste" words, such as "Studies on, ""Investigations on," and "Observations on." An opening "A," "An," or "The" is alos a waste word.
        The title of a paper is a label, In other words, the title normally is not a sentence, so it is simpler than a sentence, but the order of the words became even more important.
        Titles should almost never contain abbreviations, chemical formulas, proprietary names, jargon.

    Abstract:
        Abstract is a miniature version of the paper. The Abstract should provide a brief summary of each of the main section of the paper: Introduction, Materials and Methods, Results, and Discussion. It should not exceed the length specified by the journal. So the main requirement of Abstract is enables reader identify the basic content of a paper quickly and accurately. Typically An Abstract should be typed as a single paragraph.
        The Abstract should :
    • State the principal objectives and scope of the investigation
    • Describe the methods employed
    • Summarize the results
    • State the principal conclusions (indicated by the fact that they are often given three times: once in the Abstract, again in the Introduction, and again in the Discussion.
        There are some types of Abstracts such as: informative abstract, indicative abstract.
        A informative abstract should briefly state the problem, the method used to study the problem, and the principal data and conclutions. this type of abstract require readers read the full paper.
        A indicative abstract is designed to indicate the subjects dealt with in a paper, this allow potential readers to decide whether to read the paper. It can seldom serve as a substitute for the full paper, but just be used in other types of publications.

    2009-03-02

    ffmpeg使用x264编码的配置 + ffmpeg与 x264编码器参数完整对照表

      使用ffmpeg进行.264编码的相关文章比较少,google了一下,特总结如下:

    • qscale的取值可以是0.01-255但实际使用超过50就很糟糕了
    • ffmpeg的cbr模式可以把码率控制的不错,但是vbr无法限制最高码率(虽然有max的设置,但是程序没有实现)
    • x264标准的封装是x264+aac in flv或者x264+aac in MP4

    接下来说明下ffmpeg命令行的语法规则(本块内容来自2009-03-02官方文档):
    语法规则结构:
    ffmpeg [[infile options][`-i' infile]]... {[outfile options] outfile}...
    一个最简单的命令形式:
    ffmpeg -i input.avi -b 64k output.avi

    这个命令把视频以64k的码率重编码。
    显然,输入文件前面要加一个-i选项下面介绍一些有用的全局参数:
    • -formats 参数。会显示你机器当前支持的封装、编码、解码器的信息
    • -y参数,会指示ffmpeg覆盖输出文件
    • -t 指定视频流持续的时常,支持以秒为单位的数字或"时:分:秒[.毫秒]"
    • -fs 指定输出文件大小的限制
    • -ss 指定开始的时间,和-t的单位一样
    • -target 直接设定你想要转成的目标格式,所有的相关设置都会采用内设值,当然也你也可以加上自己要修改的参数。可用的选择有:
      "vcd", "svcd", "dvd", "dv", "dv50", "pal-vcd", "ntsc-svcd", ...
      这个例子把视频转换成vcd的格式
      ffmpeg -i myfile.avi -target vcd /tmp/vcd.mpg
    接下来介绍视频选项:
    • -b 指定码率注意单位是bit/s,所以我们一般要加k,比如 -b 1000k 就是1000kb/s
    • -g 设置组的大小
    • -vframes 指定要编码的帧数,比如-vframes 1 就是编码1帧,截图的时候就这样写.
    • -r 指定帧率,默认是25
    • -s 指定图像分辨率,用wxh的格式,比如320x240
    • -aspect 指定宽高比 可以些16:9这种,也可以写小数比如1.3333
    • -croptop 指定顶部裁减多少像素,类似的还有-cropleft -cropright -cropbuttom
    • -bt 设置比特率容许的误差,默认4000k,在第一阶段时使用这个参数会告诉码率控制器能够偏移平均码率多远,这个选项和最大最小码率无关.设太小了不利于质量
    • -maxrate 和-minrate 指定允许的最大和最小码率,一般如果要用cbr模式编码的话会用这个:
      ffmpeg -i myfile.avi -b 4000k -minrate 4000k -maxrate 4000k -bufsize 1835k out.m2v
      否则用处不大
    • -vcodec 强制使用某种编码器
    • -sameq 使用和源文件相同的质量,这个选项是动态码率的
    • -pass 指定编码阶段,这个只有1和2,第一阶段的时候应该不处理音频,并且把输出导向空,比如:
      ffmpeg -i foo.mov -vcodec libxvid -pass 1 -an -f rawvideo -y NUL
      ffmpeg -i foo.mov -vcodec libxvid -pass 1 -an -f rawvideo -y /dev/null
    • -qscale 使用固定量化因子来量化视频这个是在vbr模式的,前面有提到,越小质量越好,不要超过50,相关的参数还有-qmin --qmax用来设定最大最小可使用的量化值
    • -qdiff 指定固定量化器因子允许的最大偏差
    • -qblur 指定量化器模糊系数,可用范围是0.0-1.0越大使得码率在时间上分配的越平均
    • -qcomp 指定视频量化器压缩系数,默认0.5
    • -me_method 指定运动估计方法(motion estimation method),可用的选择从垃圾到好排列如下:
      zero (0向量)
      phods
      log
      x1
      hex
      umh
      epzs (默认)
      full (完全搜索,很慢又好不到哪里去)
    • -mbd 设定宏模块决策,可用的值:
      0 使用mb_cmp,不知道是什么东西,所以这些参数我参考一下mencoder里面的
      1 选用需要比特最少的宏块模式
      2 选用码率失真最优的宏块模式
    • -4mv 使用宏块的4个运动向量,只支持mpeg4
    • -part 使用数据划分,只支持mpeg4
    • -ilme 强制允许交错的编码,只支持mpeg2和mpeg4,你可以选择用-deinterlace来去交错
    音频部分:
    • -ar 设置采样频率,默认44100hz
    • -ab 设置比特率,默认64k
    • -an 禁用音频录制
    • -acodec 指定音频编码器


    下面举几个x264编码的例子:
    我使用mencoder调用x264编码一个psp用的视频:
    x264+aac in mp4 for psp
    mencoder -oac lavc -lavcopts acodec=libfaac:abitrate=94 -ovc x264 -x264encopts cabac=1:ref=3:deblock=1,-1,-1:analyse=0x1:0x111:me=umh:subme=9:psy_rd=0.9,0.0:mixed_ref=1:me_range=32:chroma_me=1:trellis=2:8x8dct=0:cmp=0:deadzone=21,11:chroma_qp_offset=-2:nr=0:decimate=0:mbaff=0:bframes=2:b_pyramid=0:b_adapt=1:b_bias=0:direct=3:wpredb=1:keyint=250:keyint_min=25:scenecut=40:rc=cqp:qp=18:ip_ratio=1.40:pb_ratio=1.30:aq=0 -lavdopts er=2 -pp 6 -of lavf -lavfopts format=psp -vf scale=480:-3
    对应的ffmpeg编码参数是:
    ffmpeg
    ffmpeg -i inputfile.avi -f psp -acodec libfaac -ab 94k -vcodec libx264 -cqp 28 -coder 1 -refs 3 -deblockalpha 1 -deblockbeta -1 -me_method umh -subq 9 -me_range 32 -trellis 2 -chromaoffset -2 -nr 0 -bf 2 -b_strategy 1 -bframebias 0 -directpred 3 -g 250 -i_qfactor 1.3 -b_qfactor 1.4 -flags2 +bpyramid+wpred+mixed_refs+8x8dct -er 2 -s 480x320
    需要注意的是,flags2里面那块,似乎要按照一定顺序才能正常工作,其他地方都差不多,详细情况可以从下面两篇文章得出:
    第一篇是这个在网上被传了很多,但有些问题的对照表格,我修改了有问题的部分:
    FFmpeg option x264 option
    -g <frames> --keyint
    -b <bits per second> --bitrate
    -bufsize <bits> --vbv-bufsize
    -maxrate <bits> --vbv-maxrate
    -pass <1,2,3> --pass
    -crf <float> --crf
    -cqp <int> --qp
    -bf <int> --bframes
    -coder <0,1> --no-cabac
    -bframebias <int> --b-bias
    -keyint_min <int> --min-keyint
    -sc_threshold <int> --scenecut
    -deblockalpha <int>-deblockbeta <int> --deblock
    -qmin <int> --qpmin
    -qmax <int> --qpmax
    -qdiff <int> --qpstep
    -qcomp <float> --qcomp
    -qblur <float> --qblur
    -complexityblur <float> --cplxblur
    -refs <int> --ref
    -directpred <int> --direct
    -me_method <epzs,hex,umh,full> --me
    -me_range <int> --merange
    -subq <int> --subme
    -bidir_refine <0,1> --bime
    -trellis <0,1,2> --trellis
    -nr <int> --nr
    -level <int> --level
    -bt <bits> --ratetol = -bt / -b
    -rc_init_occupancy <bits> --vbv-init = -rc_init_occupancy / -bufsize
    -i_qfactor <float> --ipratio = 1 / -i_qfactor
    -b_qfactor <float> --pbratio
    -chromaoffset <int> --chroma-qp-offset
    -rc_eq <string> --rc_eq
    -threads <int> --threads
    -cmp <-chroma/+chroma> --no-chroma-me
    -partitions --partitions
    +parti8×8 i8×8
    +parti4×4 i4×4
    +partp8×8 p8×8
    +partp4×4 p4×4
    +partb8×8 b8×8
    -flags
    -loop/+loop --no-deblock/–deblock
    -psnr/+psnr --no-psnr/nothing
    -flags2
    +bpyramid --b-pyramid
    +wpred --weightb
    +brdo --b-rdo 我这里的ffmpeg已经不能用这个了
    +mixed_refs --mixed-refs
    +dct8×8 --8×8dct
    -fastpskip/+fastpskip --no-fast-pskip
    +aud --aud


    下面是一篇关于这些参数详细解释的文章,转自:http://ffmpeg.x264.googlepages.com/mapping
    一下是这篇文章在2008年11月19日更新的版本:

    Frame-type options:

    • --keyint <integer> (x264)
      -g <integer>
      (FFmpeg)
      Keyframe interval, also known as GOP length. This determines the maximum distance between I-frames. Very high GOP lengths will result in slightly more efficient compression, but will make seeking in the video somewhat more difficult. Recommended default: 250

    • --min-keyint <integer> (x264)
      -keyint_min <integer> (FFmpeg)
      Minimum GOP length, the minimum distance between I-frames. Recommended default: 25

    • --scenecut <integer> (x264)
      -sc_threshold <integer> (FFmpeg)
      Adjusts the sensitivity of x264's scenecut detection. Rarely needs to be adjusted. Recommended default: 40

    • --pre-scenecut (x264)
      UNKNOWN (FFmpeg)
      Slightly faster (but less precise) scenecut detection. Normal scenecut detection decides whether a frame is a scenecut after the frame is encoded, and if so then re-encodes the frame as an I-frame. This is not compatible with threading, however, and so --pre-scenecut is automatically activated when multiple encoding threads are used.

    • --bframes <integer> (x264)
      -bf <integer> (FFmpeg)
      B-frames are a core element of H.264 and are more efficient in H.264 than any previous standard. Some specific targets, such as HD-DVD and Blu-Ray, have limitations on the number of consecutive B-frames. Most, however, do not; as a result, there is rarely any negative effect to setting this to the maximum (16) since x264 will, if B-adapt is used, automatically choose the best number of B-frames anyways. This parameter simply serves to limit the max number of B-frames. Note that Baseline Profile, such as that used by iPods, does not support B-frames. Recommended default: 16

    • --b-adapt <integer> (x264)
      -b_strategy <integer> (FFmpeg)
      x264, by default, adaptively decides through a low-resolution lookahead the best number of B-frames to use. It is possible to disable this adaptivity; this is not recommended. Recommended default: 1
    • 0: Very fast, but not recommended. Does not work with pre-scenecut (scenecut must be off to force off b-adapt).

      1: Fast, default mode in x264. A good balance between speed and quality.

      2: A much slower but more accurate B-frame decision mode that correctly detects fades and generally gives considerably better quality. Its speed gets considerably slower at high bframes values, so its recommended to keep bframes relatively low (perhaps around 3) when using this option. It also may slow down the first pass of x264 when in threaded mode.

    • --b-bias 0 (x264)
      -bframebias 0 (FFmpeg)
      Make x264 more likely to choose higher numbers of B-frames during the adaptive lookahead. Not generally recommended. Recommended default: 0

    • --b-pyramid (x264)
      -flags2 +bpyramid (FFmpeg)
      Allows B-frames to be kept as references. The name is technically misleading, as x264 does not actually use pyramid coding; it simply adds B-references to the normal reference list. B-references get a quantizer halfway between that of a B-frame and P-frame. This setting is generally beneficial, but it increases the DPB (decoding picture buffer) size required for playback, so when encoding for hardware, disabling it may help compatibility.

    • --no-cabac (x264)
      -coder 0,1 (FFmpeg)
      CABAC is the default entropy encoder used by x264. Though somewhat slower on both the decoding and encoding end, it offers 10-15% improved compression on live-action sources and considerably higher improvements on animated sources, especially at low bitrates. It is also required for the use of trellis quantization. Disabling CABAC may somewhat improve decoding performance, especially at high bitrates. CABAC is not allowed in Baseline Profile. Recommended default: -coder 1 (CABAC enabled)

    • --ref <integer> (x264)
      -refs <integer> (FFmpeg)
      One of H.264's most useful features is the abillity to reference frames other than the one immediately prior to the current frame. This parameter lets one specify how many references can be used, through a maximum of 16. Increasing the number of refs increases the DPB (Decoded Picture Buffer) requirement, which means hardware playback devices will often have strict limits to the number of refs they can handle. In live-action sources, more reference have limited use beyond 4-8, but in cartoon sources up to the maximum value of 16 is often useful. More reference frames require more processing power because every frame is searched by the motion search (except when an early skip decision is made). The slowdown is especially apparent with slower motion estimation methods. Recommended default: -refs 6

    • --no-deblock (x264)
      -flags -loop (FFmpeg)
      Disable loop filter. Recommended default: -flags +loop (Enabled)

    • --deblock (x264)
      -deblockalpha <integer>(FFmpeg)
      -deblockbeta <integer>(FFmpeg)
      One of H.264's main features is the in-loop deblocker, which avoids the problem of blocking artifacts disrupting motion estimation. This requires a small amount of decoding CPU, but considerably increases quality in nearly all cases. Its strength may be raised or lowered in order to avoid more artifacts or keep more detail, respectively. Deblock has two parameters: alpha (strength) and beta (threshold). Recommended defaults:-deblockalpha 0 -deblockbeta 0 (Must have '-flags +loop')

    • --interlaced (x264)
      UNKNOWN (FFmpeg)
      Enables interlaced encoding. x264's interlaced encoding is not as efficient as its progressive encoding; consider deinterlacing for maximum effectiveness.

    Ratecontrol:

    • --qp <integer>(x264)
      -cqp <integer>(FFmpeg)
      Constant quantizer mode. Not exactly constant completely--B-frames and I-frames have different quantizers from P-frames. Generally should not be used, since CRF gives better quality at the same bitrate.

    • --bitrate <integer>(x264)
      -b <integer>(FFmpeg)
      Enables target bitrate mode. Attempts to reach a specific bitrate. Should be used in 2-pass mode whenever possible; 1-pass bitrate mode is generally the worst ratecontrol mode x264 has.

    • --crf <float>(x264)
      -crf <float>(FFmpeg)
      Constant quality mode (also known as constant ratefactor). Bitrate corresponds approximately to that of constant quantizer, but gives better quality overall at little speed cost. The best one-pass option in x264.

    • --vbv-maxrate <integer>(x264)
      -maxrate <integer>(FFmpeg)
      Specifies the maximum bitrate at any point in the video. Requires the VBV buffersize to be set. This option is generally used when encoding for a piece of hardware with bitrate limitations.

    • --vbv-bufsize <integer>(x264)
      -bufsize <integer> (FFmpeg)
      Depends on the profile level of the video being encoded. Set only if you're encoding for a hardware device.

    • --vbv-init <float>(x264)
      -rc_init_occupancy <float>(FFmpeg)
      Initial VBV buffer occupancy. Note: Don't mess with this.

    • --qpmin <integer> (x264)
      -qmin <integer> (FFmpeg)
      Minimum quantizer. Doesn't need to be changed. Recommended default: -qmin 10

    • --qpmax <integer> (x264)
      -qmax <integer> (FFmpeg)
      Maximum quantizer. Doesn't need to be changed. Recommended default: -qmax 51

    • --qpstep <integer> (x264)
      -qdiff <integer> (FFmpeg)
      Set max QP step. Recommended default: -qdiff 4

    • --ratetol <float>(x264)
      -bt <float>(FFmpeg)
      Allowed variance of average bitrate

    • --ipratio <float>(x264)
      -i_qfactor <float>(FFmpeg)
      Qscale difference between I-frames and P-frames.

    • --pbratio <float>(x264)
      -b_qfactor <float>(FFmpeg)
      Qscale difference between P-frames and B-frames.

    • --chroma-qp-offset <integer>(x264)
      -chromaoffset <integer>(FFmpeg)
      QP difference between chroma and luma.

    • --aq-strength <float> (x264)
      UNKNOWN (FFmpeg)
      Adjusts the strength of adaptive quantization. Higher values take more bits away from complex areas and edges and move them towards simpler, flatter areas to maintain fine detail. Default: 1.0

    • --pass <1,2,3> (x264)
      -pass <1,2,3>(FFmpeg)
      Used with --bitrate. Pass 1 writes the stats file, pass 2 reads it, and 3 both reads and writes it. If you want to use three pass, this means you will have to use --pass 1 for the first pass, --pass 3 for the second, and --pass 2 or 3 for the third.

    • --stats <string>(x264)
      UNKNOWN (FFmpeg)
      Allows setting a specific filename for the firstpass stats file.

    • --rceq <string>(x264)
      -rc_eq <string>(FFmpeg)
      Ratecontrol equation. Recommended default: -rc_eq 'blurCplx^(1-qComp)'

    • --qcomp <float>(x264)
      -qcomp <float> (FFmpeg)
      QP curve compression: 0.0 => CBR, 1.0 => CQP. Recommended default: -qcomp 0.60

    • --cplxblur <float>(x264)
      -complexityblur <float>(FFmpeg)
      Reduce fluctuations in QP (before curve compression) [20.0]

    • --qblur <float>(x264)
      -qblur <float>(FFmpeg)
      Reduce fluctuations in QP (after curve compression) [0.5]

    • --zones /(x264)
      UNKNOWN (FFmpeg)
      Allows setting a specific quantizer for a specific region of video.

    • --qpfile (x264)
      UNKNOWN (FFmpeg)
      Allows one to read in a set of frametypes and quantizers from a file. Useful for testing various encoding options while ensuring the exact same quantizer distribution.

    Analysis:

    • --partitions <string> (x264)
      -partitions <string> (FFmpeg)

      p8x8 (x264) /+partp8x8 (FFmpeg)

      p4x4 (x264) /+partp4x4 (FFmpeg)

      b8x8 (x264) /+partb8x8 (FFmpeg)

      i8x8 (x264) /+parti8x8 (FFmpeg)

      i4x4 (x264) /+parti4x4 (FFmpeg)

      One of H.264's most useful features is the ability to choose among many combinations of inter and intra partitions. P-macroblocks can be subdivided into 16x8, 8x16, 8x8, 4x8, 8x4, and 4x4 partitions. B-macroblocks can be divided into 16x8, 8x16, and 8x8 partitions. I-macroblocks can be divided into 4x4 or 8x8 partitions. Analyzing more partition options improves quality at the cost of speed. The default is to analyze all partitions except p4x4 (p8x8, i8x8, i4x4, b8x8), since p4x4 is not particularly useful except at high bitrates and lower resolutions. Note that i8x8 requires 8x8dct, and is therefore a High Profile-only partition. p8x8 is the most costly, speed-wise, of the partitions, but also gives the most benefit. Generally, whenever possible, all partition types except p4x4 should be used.

    • --direct <integer>(x264)
      -directpred <integer>(FFmpeg)
      B-frames in H.264 can choose between spatial and temporal prediction mode. Auto allows x264 to pick the best of these; the heuristic used is whichever mode allows more skip macroblocks. Auto should generally be used.

    • --direct-8x8 (x264)
      UNKONWN (FFmpeg)
      This should be left at the default (-1).

    • --weightb (x264)
      -flags2 +wpred(FFmpeg)
      This allows B-frames to use weighted prediction options other than the default. There is no real speed cost for this, so it should always be enabled.

    • --me (x264)
      -me_method (FFmpeg)

      dia (x264) / epzs (FFmpeg) is the simplest search, consisting of starting at the best predictor, checking the motion vectors at one pixel upwards, left, down, and to the right, picking the best, and repeating the process until it no longer finds any better motion vector.

      hex (x264) / hex (FFmpeg) consists of a similar strategy, except it uses a range-2 search of 6 surrounding points, thus the name. It is considerably more efficient than DIA and hardly any slower, and therefore makes a good choice for general-use encoding.

      umh (x264) / umh (FFmpeg) is considerably slower than HEX, but searches a complex multi-hexagon pattern in order to avoid missing harder-to-find motion vectors. Unlike HEX and DIA, the merange parameter directly controls UMH's search radius, allowing one to increase or decrease the size of the wide search.

      esa (x264) / full (FFmpeg) is a highly optimized intelligent search of the entire motion search space within merange of the best predictor. It is mathematically equivalent to the bruteforce method of searching every single motion vector in that area, though faster. However, it is still considerably slower than UMH, with not too much benefit, so is not particularly useful for everyday encoding.

      One of the most important settings for x264, both speed and quality-wise.

    • --merange <integer>(x264)
      -me_range <integer>(FFmpeg)
      MErange controls the max range of the motion search. For HEX and DIA, this is clamped to between 4 and 16, with a default of 16. For UMH and ESA, it can be increased beyond the default 16 to allow for a wider-range motion search, which is useful on HD footage and for high-motion footage. Note that for UMH and ESA, increasing MErange will significantly slow down encoding.

    • --mvrange(x264)
      UNKNOWN (FFmpeg)
      Limits the maximum motion vector range. Since x264 by default limits this to 511.75 for standards compliance, this should not be changed.

    • --subme 6(x264)
      -subq 6(FFmpeg)

      1: Fastest, but extremely low quality. Should be avoided except on first pass encoding.

      2-5: Progressively better and slower, 5 serves as a good medium for higher speed encoding.

      6-7: 6 is the default. Activates rate-distortion optimization for partition decision. This can considerably improve efficiency, though it has a notable speed cost. 6 activates it in I/P frames, and subme7 activates it in B frames.

      8-9: Activates rate-distortion refinement, which uses RDO to refine both motion vectors and intra prediction modes. Slower than subme 6, but again, more efficient.

      An extremely important encoding parameter which determines what algorithms are used for both subpixel motion searching and partition decision.

    • --psy-rd <float>:<float> (x264)
      UNKNOWN (FFmpeg)
      First value represents the amount that x264 biases in favor of detail retention instead of max PSNR in mode decision. Requires subme >= 6. Second value is psy-trellis, an experimental algorithm that tries to improve sharpness and detail retention at the expense of more artifacting. Recommended starting values are 0.1-0.2. Requires trellis >= 1. Recommended default: 1.0:0.0

    • --mixed-refs(x264)
      -flags2 +mixed_refs(FFmpeg)
      H.264 allows p8x8 blocks to select different references for each p8x8 block. This option allows this analysis to be done, and boosts quality with little speed impact. It should generally be used, though it obviously has no effect with only one reference frame.

    • --no-chroma-me(x264)
      UNKNOWN (FFmpeg)
      Chroma is used in the last steps of the subpixel refinement by default. For a slight speed increase, this can be disabled (at the cost of quality).

    • --8x8dct (x264)
      -flags2 +dct8x8(FFmpeg)
      Gives a notable quality boost by allowing x264 to choose between 8x8 and 4x4 frequency transform size. Required for i8x8 partitions. Speed cost for this option is near-zero both for encoding and decoding; the only reason to disable it is when one needs support on a device not compatible with High Profile.

    • --trellis <0,1,2>(x264)
      -trellis <0,1,2>(FFmpeg)

      0: disabled

      1: enabled only on the final encode of a MB

      2: enabled on all mode decisions

      The main decision made in quantization is which coefficients to round up and which to round down. Trellis chooses the optimal rounding choices for the maximum rate-distortion score, to maximize PSNR relative to bitrate. This generally increases quality relative to bitrate by about 5% for a somewhat small speed cost. It should generally be enabled. Note that trellis requires CABAC.

    • --no-fast-pskip(x264)
      -flags2 -fastpskip(FFmpeg)
      By default, x264 will skip macroblocks in P-frames that don't appear to have changed enough between two frames to justify encoding the difference. This considerably speeds up encoding. However, for a slight quality boost, P-skip can be disabled. In this case, the full analysis will be done on all P-blocks, and the only skips in the output stream will be the blocks whose motion vectors happen to match that of the skip vector and motion vectors happen to match that of the skip vector and which have no residual. The speed cost of enabling no-fast-pskip is relatively high, especially with many reference frames. There is a similar B-skip internal to x264, which is why B-frames generally encode much faster than P-frames, but it cannot be disabled on the commandline.

    • --no-dct-decimate(x264)
      UNKNOWN (FFmpeg)
      By default, x264 will decimate (remove all coefficients from) P-blocks that are extremely close to empty of coefficents. This can improve overall efficiency with little visual cost, but may work against an attempt to retain grain or similar. DCT decimation should be left on unless there's a good reason to disable it.

    • --nr(x264)
      UNKNOWN (FFmpeg)
      a fast, built-in noise reduction routine. Not as effective as external filters such as hqdn3d, but faster. Since x264 already naturally reduces noise through its quantization process, this parameter is not usually necessary.

    • --deadzone-inter(264)
      --deadzone-intra (x264)
      UNKNOWN (FFmpeg)
      UNKNOWN (FFmpeg)
      When trellis isn't activated, deadzone parameters determine how many DCT coefficients are rounded up or down. Rounding up results in higher quality and more detail retention, but costs more bits--so rounding is a balance between quality and bit cost. Lowering these settings will result in more coefficients being rounded up, and raising the settings will result in more coefficients being rounded down. Recommended: keep them at the defaults.

    • --cqm (264)
      --cqpfile(x264)UNKNOWN (FFmpeg)
      UNKNOWN (FFmpeg)
      Allows the use of a custom quantization matrix to weight frequencies differently in the quantization process. The presets quant matrices are "jvt" and "flat". --cqpfile reads a custom quant matrices from a JM-compatible file. Recommended only if you know what you're doing.

    2009-03-01

    桑梓贴吧的flv封装的h.264编码环境部署

    使用:

    • faac
    • faad
    • x264
    • mplayer
    • ffmpeg
    • amrwb+amrnb+26204-700.zip+26104-700.zip(用以解码3gp视频)
    编译:
    faac:
    ./configure
    make
    make install

    faad:
    ./configure
    make
    make install

    x264:
    ./configure --enable-shared
    make
    make install

    mplayer和ffmpeg下载每日完全快照:
    http://www.mplayerhq.hu/MPlayer/releases/mplayer-export-snapshot.tar.bz2
    http://www.ffmpeg.org/releases/ffmpeg-checkout-snapshot.tar.bz2


    amrwb+amrnb+26204-700.zip+26104-700.zip:
      amrwb是3gp的amr解码器,后面那两个zip文件是3gpp的ts的标准的源代码,对这个的解释如下:
    The library itself is actually just a wrapper around the original code
    released by 3GPP.

      如果你的服务器不能连接外网,需要把这两个文档从
    http://www.3gpp.org/ftp/Specs/archive/26_series/26.204/26204-700.zip
    http://www.3gpp.org/ftp/Specs/archive/26_series/26.104/26104-700.zip
      这两个地址下载来分别放在amrwb和amrnb的源代码的根目录下。
    ./configure
    make
    make install

    ffmpeg:
    ./configure --prefix=/opt/DistributedConvertProgram/mediaPrograms/ffmpeg --enable-libfaac --enable-libfaad --enable-libx264 --enable-gpl --enable-shared --enable-libamr-nb --enable-libamr-wb --enable-nonfree

    make
    make install


    简单说明:
      x264要使用--enable-shared的原因是ffmpeg编译的时候需要libx264.so
      ffmpeg使用--enable-shared的原因也是如此.

    最后:
      注意,你可能要修改/etc/ld.so.conf这个文件,在其中加入对应的lib地址,并ldconfig才能保证不出现*.so文件找不到的情况

    ---------------------------------------------------------------------------------
    09-03-11更新:ffmpeg编译时未添加amr支持,导致不支持3gp解码,已添加在上面.

    编译安装Mencoder和FFMpeg

      由于这两个程序现在都基本不提供所谓的release了,很多新的功能必须使用svn的代码来编译安装,其实本来编译很简单,不过按照我们的需求来编有时候就会麻烦了。。
    编译准备:
      先安装其他编码器:
      FAAC和FAAD:https://sourceforge.net/project/showfiles.php?group_id=704
      LAME:https://sourceforge.net/project/showfiles.php?group_id=290
      Theora:http://downloads.xiph.org/releases/theora/
      vorbis:http://downloads.xiph.org/releases/vorbis/
      x264:http://www.videolan.org/developers/x264.html

    下载完以后一个个编译好。x264需要yasm
    之后再编译mplayer和ffmpeg
    mplayer最简单了,如果你不需要gui的话直接
    ./configure --prefix=dir_you_want
    就可以了。不过注意,如果你编译的时候发现很多codec不支持的话记得要指定解码器目录
    ffmpeg会麻烦一点,你编译ffmpeg的时候,可能需要
    ./configure --prefix=dir_you_want --enable-libmp3lame --enable-libvorbis --enable-libxvid --enable-shared --enable-libfaac --enable-libfaad --enable-gpl --enable-libtheora --enable-libx264
    编译完成之后,跑ffmpeg发现
    ffmpeg: error while loading shared libraries: libavdevice.so.52: cannot open shared object file
    这样的错误,那有两种解决方法。
    一个是把你用prefix指定的安装目录下的lib目录里面的文件link到/usr/lib中去。
    另外一个是修改/etc/ld.so.conf
    添加你prefix指定的目录的lib目录的绝对路径。
    然后ldconfig即可。

    ------------------------------------------------------------------------
    更新:
      如果你在使用libx264编码的时候遇到这样的错误:

    [libx264 @ 0x9d515e0]no ratecontrol method specified

    那么,80%的概率是你编译x264的时候使用了--enable-shared。这个选项引发了这个错误。具体原因似乎是没能加载所需要的动态库。
    但是问题是,如果你想要ffmpeg能够使用libx264的话又必须要使用libx264.so.66这个动态库。否则你不能编译ffmpeg。所以这个问题相当奇怪。
    我是在编译完ffmpeg之后重新编译x264才搞定的。
    ------------------------------------------------------------------------
    更新:在另一台机器上编译过程不同于此,因此,本帖可能有错,待研究一下动态编译.

    2009-02-22

    Windows Server 2008的注册表结构

    Windows Server 2008的注册表结构:

    • HKEY_CLASSES_ROOT:用于管理文件系统,指定了Windows中某扩展名的应用程序的文件类型和打开这个文件要调用的程序。和HKEY_LOCAL_MACHINE\SOFTWARE\Classes是一样的,另外简称HKCR。详细文档:http://msdn.microsoft.com/en-us/library/ms724475(VS.85).aspx
    • HKEY_CURRENT_USER:包含系统当前用户的信息,在用户登录系统的时候从HKEY_USERS里把对应的项拷贝到HKEY_CURRENT_USER拷贝过来。
    • HKEY_LOCAL_MACHINE:管理当前系统的硬件配置,是对所有用户都有效的设置,简称HKLM
    • HKEY_USERS:管理系统的用户信息。在这个根键中保存了本地口令列表中的用户标识和密码列表,同时每个用户的预配置信息都被保存在这里。
    • HKEY_CURRENT_CONFIG:管理当前用户的系统配置。在这个根键中,保存着定义当前用户桌面配置的数据,改用户使用过的文档列表等等。

    常用子键:

    • HKLM\SOFTWARE\Microsoft\Windows\currentVersion\Uninstall:程序的卸载信息
    • HKLM\SYSTEM\currentControlSet\Control\keyboard Layout:当前语言和输入法信息
    • HKLM\SYSTEM\CurrentControlSet\Services\Class:保存计算机设备类型目录,就是整台机器的硬件信息
    • HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer:\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders:保存计算机中个人文件夹收藏夹的路径
    • HKLM\SOFTWART\Microsoft\Windows\CurrentVersion\Run:保存由控制面板设定的启动时运行程序的名称

    注册表的键值格式为:键值名:数据类型:键值

      其中键值可以是:

    • 字符串值(S):一般表示文件的描述硬件标识什么的。
    • 二进制值(B):无长度限制。以HEX的形式显示
    • DWORD值(D):32位长的数值

    REG文件格式:

    很简单,你可以直接导出来学习。

    Windows Registry Editor Version 5.00

    [HKEY_USERS\.DEFAULT\Software\Microsoft\Windows\CurrentVersion\Internet Settings\5.0\Cache\Content]@=”tes“
    "CachePrefix"="00000test"
    "CacheLimit"=dword:0000c800"SavedLegacySettings"=hex:46,00,00,00,1f,00,00,00,09,00,00,00,00,00,00,00,00,\00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,\
      00,00,00,00,00,00,00,00,00,00,00,00,00,00
    "testAddress"="C:\\regedit\\test.reg"
    "testAddress"=-

    使用REG文件可以操作主键和键值。

    • 添加主键直接在[]里面打出主键的路径即可比如[HKEY_USERS\.DEFAULT\Software\Microsoft\test]
    • 删除只需要在前面加个-即可,比如:[-HKEY_USERS\.DEFAULT\Software\Microsoft\test],注意删除的时候会把整个主键下所有东西都删掉。
    • 添加键值:添加字符串直接用双引号引起来,就像上面那个"CachePrefix"="00000test"一样,添加DWORD数据需要dwork:date,就像上面一样。如果添加二进制数据则是hex:date上面那个\是继续下一行的意思。要设置默认值的键值数据就需要用@符号。
    • 删除键值:只需要写”键值“=-即可,比如上面最后一句就是删除testAddress键



    2009-02-21

    [转]总结几条用Word排论文的技巧

      插公式时经常需要让公式居中对齐、行末的公式编号右对齐。只要设一个居中制表位和一个右对齐制表位,输入时按TAB即可。建议居中制表位位置用字符做单位,比如五号字体在缺省A4页面上是每行40字符,居中制表位就可以设在20字符处。
      我的公式编号用SEQ域输入,从“插入”菜单选“域”即可。这样做的好处是如果我有100个公式,在最开始插入一个新的,那么这100个公式的编号都要加1。
      如果手工修改会非常耗体,而Ctrl+A全部选定再F9更新域则十分快捷。
      参考文献用尾注,也在“插入”菜单里。缺省的是上标格式,按Ctrl+'+'可以改成正常格式。好处也是可以自动排号。
      图片说明用题注。一般排版要让题注宽度小于版心距,我用水平标尺上的悬挂缩进加右缩进实现。
      Word表格中可以进行简单的计算。比如我有一个表格,让第三列的值显示第二列比第一列多百分之几,用“表格”菜单的“公式”即可,不用Excel高射炮轰蚊子。
      对公式、尾注、图片、表格的引用一概用交叉引用,“插入”菜单下。好处是可以做成超级链接,而且公式编号改动的话对这个公式的引用会跟着改。
      所有的章节标题都用样式,并且用多级项目符号列表。好处是标题编号(第几章、第几节)可以自动生成,可以在文档结构图(“视图”菜单)中浏览,可以用垂直滚动条下方的定位按钮快速定位,还可以生成目录。我一般关闭“基于所用格式定义样式”这个自作聪明的功能,所有样式都自己选。
      我的图片和表格做成嵌入式的,这样不会乱跑。表格每个单元格水平垂直均居中对齐(Word 2000新增功能)。表格段落格式选“段中不分页”,这样不会出现表格拆到两页上的恶心局面。
    我的正文缺省样式是首行缩进2字符。输公式的时候要另起一行,但是逻辑上它应该跟前面的文字算作一段(鼠标三击可以选定段),我用Shift+Enter 小回车搞定。每次要输入公式的时候,点到前一个包含公式的段,用格式刷复制即可。 先写这么多,以后有新的再补充,也欢迎大家补充。
      用Word编辑论文的几个建议 由于各方面的原因,大家主要还是用Microsoft Word (以下简称Word)编辑论文。Word在写科技论文方面虽然有一些先天不足,但却提供了非常强大的功能。如果不能充分利用这些功能,可能经常要为不断地调整格式而烦恼。我把自己以前使用Word的经验和教训总结一下,抛块砖。
      原则: 内容与表现分离 一篇论文应该包括两个层次的含义:内容与表现,前者是指文章作者用来表达自己思想的文字、图片、表格、公式及整个文章的章节段落结构等,而后者则是指论文页面大小、边距、各种字体、字号等。相同的内容可以有不同的表现,例如一篇文章在不同的出版社出版会有不同的表现;而不同的内容可以使用相同的表现,例如一个期刊上发表的所有文章的表现都是相同的。这两者的关系不言自明。在排版软件普及之前,作者只需关心文章的内容,文章表现则由出版社的排版工人完成,当然他们之间会有一定交互。Word 倡导一种所见即所得(WYSIWYG)的方式,将编辑和排版集成在一起,使得作者在处理内容的同时就可以设置并立即看到其表现。可惜的是很多作者滥用WYSIWYG,将内容与表现混杂在一起,花费了大量的时间在人工排版上,然而效率和效果都很差。 本文所强调的“内容与表现分离”的原则就是说文章作者只要关心文章的内容,所有与内容无关的排版工作都交给 Word 去完成,作者只需将自己的排版意图以适当的方式告诉 Word。因为Word不仅仅是一个编辑器,还是一个排版软件,不要只拿它当记事本或写字板用。主要建议如下。
      一定要使用样式,除了Word原先所提供的标题、正文等样式外,还可以自定义样式。如果你发现自己是用选中文字然后用格式栏来设定格式的,一定要注意,想想其他地方是否需要相同的格式,如果是的话,最好就定义一个样式。对于相同排版表现的内容一定要坚持使用统一的样式。这样做能大大减少工作量和出错机会,如果要对排版格式(文档表现)做调整,只需一次性修改相关样式即可。使用样式的另一个好处是可以由Word 自动生成各种目录和索引。

    1. 一定不要自己敲编号,一定要使用交叉引用。如果你发现自己打了编号,一定要小心,这极可能给你文章的修改带来无穷的后患。标题的编号可以通过设置标题样式来实现,表格和图形的编号通过设置题注的编号来完成。在写“参见第x章、如图x所示”等字样时,不要自己敲编号,应使用交叉引用。这样做以后,当插入或删除新的内容时,所有的编号和引用都将自动更新,无需人力维护。并且可以自动生成图、表目录。
    2. 一定不要自己敲空格来达到对齐的目的。只有英文单词间才会有空格,中文文档没有空格。所有的对齐都应该利用标尺、制表位、对齐方式和段落的缩进等来进行。如果发现自己打了空格,一定要谨慎,想想是否可以通过其他方法来避免。同理,一定不要敲回车来调整段落的间距。
    3. 绘图。统计图建议使用Execel生成,框图和流程图建议使用Visio画。如果不能忍受Visio对象复制到Word的速度,还可以试试 SmardDraw,功能不比Visio弱,使用不比Visio难,速度却快多了。如果使用Word的绘图工具绘图,最好以插入Word图片的方式,并适当使用组合。
    4. 编辑数学公式建议使用 MathType5.0,其实Word集成的公式编辑器是它的3.0版。安装MathType后,Word会增加一个菜单项,其功能一目了然。一定要使用 MathType 的自动编号和引用功能。这样首先可以有一个良好的对齐,还可以自动更新编号。Word 正文中插入公式的一个常见问题是把上下行距都撑大了,很不美观,这部分可以通过固定行距来修正。
    5. 参考文献的编辑和管理。如果你在写论文时才想到要整理参考文献,已经太迟了,但总比论文写到参考文献那一页时才去整理要好。应该养成看文章的同时就整理参考文献的习惯。手工整理参考文献是很痛苦的,而且很容易出错。Word没有提供管理参考文献的功能,用插入尾注的方法也很不地道。我建议使用 Reference Manager,它与Word集成得非常好,提供即写即引用(Cite while you write,简称Cwyw)的功能。你所做的只是像填表格一样地输入相关信息,如篇名、作者、年份等在文章中需要引用文献的的方插入标记,它会为你生成非常美观和专业的参考文献列表,并且对参考文献的引用编号也是自动生成和更新的。这除了可以保持格式上的一致、规范,减少出错机会外,更可以避免正文中对参考文献的引用和参考文献列表之间的不匹配。并且从长远来说,本次输入的参考文献信息可以在今后重复利用,从而一劳永逸。类似软件还有Endnote和Biblioscape。 Endnote优点在于可以将文献列表导出到BibTeX格式,但功能没有Reference Manager强大。可惜这两个软件都不支持中文,据说 Biblioscape对中文支持的很好,我没有用过,就不加评论了。
    6. 使用节。如果希望在一片文档里得到不同的页眉、页脚、页码格式,可以插入分节符,并设置当前节的格式与上一节不同。
    7. 上述7点都是关于排版的建议,还是要强调一遍,作者关心的重点是文章的内容,文章的表现就交给Word去处理。如果你发现自己正在做与文章内容无关的繁琐的排版工作,一定要停下来学一下Word的帮助,因为Word 早已提供了足够强大的功能。
    8. 我不怀疑Word的功能,但不相信其可靠性和稳定性,经常遇到“所想非所见”、“所见非所得”的情况让人非常郁闷。如果养成良好的习惯,这些情况也可以尽量避免,即使遇上,也可以将损失降低到最低限度。建议如下:
    9. 使用子文档。学位论文至少要几十页,且包括大量的图片、公式、表格,比较庞大。如果所有的内容都保存在一个文件里,打开、保存、关闭都需要很长的时间,且不保险。建议论文的每一章保存到一个子文档,而在主控文档中设置样式。这样每个文件小了,编辑速度快,而且就算文档损坏,也只有一章的损失,不至于全军覆灭。建议先建主控文档,从主控文档中创建子文档,个人感觉比先写子文档再插入到主控文档要好。
    10. 及时保存,设置自动保存,还有一有空就ctrl+s。
    11. 多做备份,不但Word不可靠,windows也不可靠,每天的工作都要有备份才好。注意分清版本,不要搞混了。Word提供了版本管理的功能,将一个文档的各个版本保存到一个文件里,并提供比较合并等功能。不过保存几个版本后文件就大得不得了,而且一个文件损坏后所有的版本都没了,个人感觉不实用。还是多处备份吧
    12. 插入的图片、和公式最好单独保存到文件里另做备份。否则,哪天打文档时发现自己辛辛苦苦的编辑的图片和公式都变成了大红叉,哭都来不及了。

      其他建议:
    13. 使用大纲视图写文章的提纲,调整章节顺序比较方便
    14. 使用文档结构图让你方便的定位章节
    15. 使用文档保护,方便文章的审阅和修改
    上面的建议并不全面,但相信比较管用。如果还有疑问,自己花些时间研究一下Word的帮助,相信会有事半功倍的效果

    2009-02-19

    Linux下转换视频文件Mencoder篇。续

      之前那篇已经太长了,再修改数据丢失的风险太大,所以就新开了一帖。如果有需要可以先看下旧的那篇

    关于h.264编码
      去年底我曾经详细的通过实际压缩出来的结果对比研究和几个星期的x264编码的效果。但是可惜的是当时忙于项目,没有记录,现在要用时发现信息已经丢掉甚多,实为可惜。所以先整理一小部分还找的到的资料。
      h.264编码几乎是现在的一种国际标准了,连那个啥dobe的flash 都能够直接解码了,就这一点就比Silverlight强了很多。
      首先看一下编码参数:
    mencoder test.avi -o /dev/null -oac copy -ovc x264 -x264encopts pass=1:turbo:me_range=16:qp_max=51:brdo:ratetol=1.0:bitrate=1000:me=umh:partitions=p8x8,b8x8,i8x8,i4x4:qp_min=10:bframes=1:qp_step=4:direct_pred=auto && mencoder test.avi -o testssdfsd.avi -oac lavc -lavcopts acodec=ac3:abitrate=96 -ovc deadzone_inter=21:pass=2:me_range=16:qcomp=0.6:qp_max=51:brdo:chroma_me:vbv_init=0.9:ratetol=1.0:bitrate=1000:me=umh:partitions=p8x8,b8x8,i8x8,i4x4:chroma_qp_offset=0:subq=5:pb_factor=1.3:deblock=0,0:qp_min=10:ip_factor=1.4:cabac:bframes=1:frameref=5:qp_step=4:b_adapt:b_bias=0:trellis=1:direct_pred=auto:nr=0:deadzone_intra=11:keyint_min=25:keyint=250

      这个是我研究出来看起来效果相对不错的编码参数,但是未必是最好的。码率的指定我没有研究,这里指定1000是为了做基准对比。
    注:以上编码参数有漏掉,待我找到原来的再补上,你可以先在ovc后面加 x264enc -x264encopts
      首先可以确定的一点是2 pass的编码效果肯定比1 pass能强上一点,主要原因是2 pass在第一轮的对视频文件的测试中能够使第二轮真正编码的时候更加充分的利用码率,就是把码率用在刀刃上。
      参数解释:第一阶段的编码,文档的解释是收集视频上的统计信息,并将这些信息写入一个文件中。 除了那些默认为打开的选项,你可能想关闭一些消耗CPU的选项。这里我使用turbo能够加快速度,本来turbo有两个值可以选的turbo=1或turbo=2,1对最终编码质量没有影响,二有一点小影响,1最大能把速度加快到2倍,2就能4倍了,由于turbo选项会减小subq、frameref的值,所以我们就没必要在第一阶段设置这两个值了。
      第二阶段的编码就可以把要的全开开了。这里不重复说明了,以前的帖子里有解释,看这个

      最后,补充一下关于需要使用flash播放264编码的相关知识,首先,官方推荐方案是h.264+aac,封装可以使用flv或mp4,但是如果你在写flex的时候两种格式似乎在加载代码上有不同,可以参考这个帖子,flv的格式标准可以从这里下载。未免那个连接挂了,这里引用一句里面最精华的内容:

    在FLEX中一般都用videoDisPlayer这个控件播放视频文件,通过使用source属性设置进行播放flv文件,如:rtmp://123.32.4.22:1935/fms/aa.flv.
    但如果播放MP4格式文件如是aa.mp4,还可以使用videoDisPlayer,但不同的地方在source赋值,应该这么写:rtmp://123.32.4.22:1935/fms/MP4:aa
    -----------------------------------------
    关于封装:
      mencoder本生支持的封装格式有限,不过通过使用libavformat获得了很多封装格式的支持。
    你可以使用
    mencoder -of help
    这个命令获得你的mencoder支持的封装格式。显然,这里相当少:
    Available output formats:
    avi - Microsoft Audio/Video Interleaved
    mpeg - MPEG-1/2 system stream format
    lavf - FFmpeg libavformat muxers
    rawvideo - (video only, one stream only) raw stream, no muxing
    rawaudio - (audio only, one stream only) raw stream, no muxing

    但是,命运是能够改变的。当你使用lavf的时候,程序会根据你的扩展名自动判别封装格式,最新的官方文档有详细的说明:
    libavformat container nameDescription
    mpgMPEG-1 and MPEG-2 PS
    asfAdvanced Streaming Format
    aviAudio-Video Interleaved
    wavWaveform Audio
    swfMacromedia Flash
    flvMacromedia Flash video
    rmRealMedia
    auSUN AU
    nutNUT open container (experimental and not yet spec-compliant)
    movQuickTime
    mp4MPEG-4 format
    dvSony Digital Video container
    mkvMatroska open audio/video container

      所以呢,结果就是,你可以任意使用这些封装格式,只需要-of lavf即可。
    /*-----------------------------------------------------*/
    *原创文章,转载请保留指向本文的连接
    /*-----------------------------------------------------*/

    2009-02-15

    Mencoder转换RMVB、wmv等视频为FLV的注意事项

    经过多日的编码参数测试,最终找到了比较不错的解决方案,趁着这次重写代码的机会,把编码参数也调整下。
    原来使用的flv编码参数是这样的:
    mencoder -of lavf -lavfopts i_certify_that_my_video_stream_does_not_use_b_frames -oac mp3lame -lameopts abr:br=56 -ovc lavc -lavcopts vcodec=flv:mbd=2:mv0:trell:v4mv:cbp:last_pred=3:dia=4:cmp=6:vb_strategy=1:vbitrate=500 -vf scale=320:240 -ofps 12 -srate 22050 /tempvideo/813f77fa8bbd055850d1b84d0e95f7e2.wmv,19:-o /raid/data_post/flvtemp/813f77fa8bbd055850d1b84d0e95f7e2.flv


      如果你阅读过mencoder的手册的话你就会发现这里有些不合理的参数,比如oac指定了mp3lame却有调用的是lameopts。经过稍微的修改之后,我采用的编码参数是这样的:

    mencoder -ovc lavc -lavcopts vcodec=flv:vbitrate=500:mbd=2:last_pred=3:cmp=3:trell:v4mv:cbp:qprd:mv0 -oac mp3lame -lameopts aq=3:preset=64 -vf expand=352::::::,scale=352:-3 -lavdopts er=2 -pp 6 -of lavf -srate 44100 -ofps 12 -mc 0 upload_save_sends/0becb057bd6a1d7f0b72feea8a032c42.avi -o /flvtemp/0becb057bd6a1d7f0b72feea8a032c42.flv


      这样稍微提高了一些视频的品质,但是用的是更多的编码时间来交换的,然而,之前在做桑梓电影的自动转换程序的时候,经过研究,发现,即使是不到1M的码率,使用h.264编码的视频能够达到非常高的清晰度,所以,最佳的解决方案还是尽快利用flash10支持h.264播放的特性更换旧的flv。

    在调试参数的过程中。用于在线视频网站的flv有如下两个问题:

    1. rmvb、wmv等视频转换时会有很多的duplicated frame,这种视频经常会造成声音画面不同步,解决方法有两个。一是,保证-ofps 是12,这样一般就能同步,如果还不行使用解决方法二:使用mencoder转换成avi容器的其他编码,再用ffmpeg转换成flv。另外,在2008年12月之后ffmpeg也加入了real video的解码支持,但是暂时还是有一点bug,所以我们等其稳定了之后就可以直接用ffmpeg编码了。
    2. flv视频在播放器上不能拖动,这是由于没有元数据的原因,使用flvtool2或者fixmdi修复一下即可。

    最近实际项目过程中的一些经验

      连续做了两个小型项目,累计了4000多行的java代码,自然也从中获得了一些实践经验。

    1. 关于java.lang.process:
      用这个类来产生子进程是不错,不过我发现只要我不获取子进程的输出流(getInputStream),我调用的mencoder就会挂死在那里,查了一下文档:创建的子进程没有自己的终端或控制台。它的所有标准 io(即 stdin,stdout,stderr)操作都将通过三个流 (getOutputStream(),getInputStream(),getErrorStream()) 重定向到父进程。因为有些本机平台仅针对标准输入和输出流提供有限的缓冲区大小,如果读写子进程的输出流或输入流迅速出现失败,则可能导致子进程阻塞,甚至产生死锁。所以,只需要接受输出流并不处理就好了,注意stdout对应的是getInputStream
    2. 远程方法调用(RMI)获得了一个服务器上的对象但是如果这个对象包含了所谓的static field 静态字段的话,客户端上这个静态类字段是空的,如果一定要用到它的话,在客户端实例化一次这个类就好。
    3. rmi获得的对象如果是null的话,会被认为是异常
    4. 扫描文件获得参数需要比较繁琐的字符串操作,应该找到一种更加简单的方法。
    5. process.waitfor()方法应该放在输入输出和错误流的后面,否则程序会一直阻塞到执行结束,而且还可能产生缓冲区满了导致调用的外部进程挂起.

    2009-02-10

    Solaris 10的信号列表和基本常识

    以下就是一张比较完整的Solaris的信号列表:


    信号名称 编号 默认处理 描述
    SIGHUP 1 Exit 挂起(参加termio(7))
    SIGINT 2 Exit 中断(参加termio(7))
    SIGQUIT 3 Core 退出(参加termio(7))
    SIGILL 4 Core 非法指令
    SIGTRAP 5 Core 跟踪或断点异常
    SIGABRT 6 Core 异常终止
    SIGEMT 7 Core 模拟异常
    SIGFPE 8 Core 浮点算数运算异常
    SIGKILL 9 Exit 杀掉进程,不能被捕获或忽略
    SIGBUS 10 Core 总线错误,不对界地址错误
    SIGSEGV 11 Core 段错误——通常是访问非法地址空间
    SIGSYS 12 Core 错误的系统调用
    SIGPIPE 13 Exit 错误管道
    SIGALRM 14 Exit 报警时钟
    SIGTERM 15 Exit 终止
    SIGUSR1 16 Exit 用户定义的信号1
    SIGUSR2 17 Exit 用户定义的信号2
    SIGHLD 18 Ignore 子进程状态改变
    SIGPWR 19 Ignore 电源失效或重新启动
    SIGWINCH 20 Ignore 窗口大小改变
    SIGURG 21 Ignore 紧急socket情况
    SIGPOLL 22 Exit 可以查询的事件(streamio(7))
    SIGIO 22 Exit aioread/aiowrite完成
    SIGSTOP 23 Stop 停止(不能被捕获或忽略)
    SIGTSTP 24 Stop 停止(作业控制)
    SIGCONT 25 Ignore 继续
    SIGTTIN 26 Stop 停止——tty输入
    SIGTTOU 27 Stop 停止——tty输出
    SIGVTALRM 28 Exit 时钟报警——setitimer(2)ITIMER_VIRTUAL报警
    SIGPROF 29 Exit Profiling报警——setitimer(2)ITIMER_PROF、ITIME_REPROF
    SIGXCPU 30 Core 超出了CPU时间限制
    SIGXFSZ 31 Core 超出了文件大小限制
    SIGWAITING 32 Ignore 并发信号,Solaris10之前的线程库使用
    SIGLWP 33 Ignore 内部LWP信号,Solaris10之前的线程库使用
    SIGFREEZE 34 Ignore 检查点挂起
    SIGTHAW 35 Ignore 检查点继续
    SIGLCANCEL 36 Ignore 撤销
    SIGLOST 37 Ignore 资源丢失
    SIGRTMIN 38 Exit 优先级最低的实时信号
    SIGRTMAX 45 Exit 优先级最高的实时信号

      对于信号,Solaris定义了4种默认的处理例程:
    • Exit:终止进程
    • Core:创建进程的内核映像文件并终止进程
    • Stop:挂起进程的执行(通常用于作业控制或调试)
    • Ignore:丢弃信号,不采取任何操作,即使信号被阻塞

    实际上,除了SIGKILL和SIGSTOP这两个信号外,程序是可以改变其他信号的处理例程的。这就具体要看程序作者是如何处理的了。

      虽然信号有这么多,但是实际上信号还分做同步信号和异步信号:

    • 同步信号也叫异常信号来自于内核的异常处理例程,担任就是程序内部有问题或者它使用的硬件有问题产生的(这里有问题不是坏掉,比如等待也可是一种)。
    • 异步信号一般来自于用户命令,程序和内核的其他地方,比如我们用kill命令发送信号给进程,或者使用快捷键Control+Z来作业控制等等

    2009-02-08

    Subversion的超快速入门

      前几天成功的在Sourceforge.net上面注册了一个项目,考虑到版本控制系统的诞生年代,我决定使用subversion。这个版本控制系统来管理源代码仓库(Repository),所以,稍微迅速的瞄了一眼svn的资料和使用方法,为了以后方便在这篇文章里稍微记下。如果你希望详细了解svn的话,请放轻松到这里去看整本书:http://www.subversion.org.cn/svnbook/

    使用SVN的必备基本概念:
      Subversion是一个“集中式”的信息共享系统。版本库是Subversion的核心部分,是数据的中央仓库(Repository)。svn把你的源代码或者任何文档集中存储在一个仓库中,以树型目录的形式维护,可以有多个用户进入这个仓库来处理源代码。这个特性使得svn看起来就像一个文件服务器一样,那它和网络硬盘有什么不一样呢?关键之处在于svn不仅能够存储你的文件,还能够按照你更新、创建文件的时间等信息来保存你的每个修订版本。你可以在有任意需求的时候倒回到任意一个修订版本,怎么样?是不是和Google Docs的功能很像?这就是它的强悍之处。当然,据说还有很多牛逼烘烘的功能 。。不过我们初学者知道这些就够了。。这个就是版本控制。
      svn使用的版本模型:
      如果你学习过基本的编程或者操作系统的进程管理的知识你就不难理解:因为有很多人可能同时在修订某个文件的自己的版本,这个过程中,就可能产生同步化的问题,某A编辑了源码保存到仓库之后某B也编辑了源码保存到仓库,这个时候某B编辑的版本就把某A编辑的版本完全覆盖了,也就是某A这位仁兄修改了半天的结果完全作废了。
      为了解决这个问题,svn使用的模型是copy-modify-merge model也就是拷贝-修改-合并模型,这个模型使得每个用户从版本库里获得一份自己的本地项目拷贝(也就是下载一份到本地)然后修改本地项目拷贝,改完以后再向仓库申请合并修改,如果在这两个修订版之间有多个用户修改这个项目,那么svn会要求用户进行检查,最终合并到新的修订版本。

    SVN的基本用法:
      好,了解了基本概念之后就可以很容易的使用svn了。一个典型的日常svn工作流程如下:

    • 更新你的工作拷贝

      • svn update :把你的工作拷贝与最新的版本同步(应该不是全部下载,而是把有被其他人更新的文件下载)

    • 做出修改(注意,这一阶段的命令都是对你本地项目拷贝的内容进行的,要更新到版本库需要你进行提交svn commit)

      • svn add :添加文件或目录或符号链接,如果你指定了一个目录,那么这个目录下的文件都会被添加,如果只想添加目录需要使用-non-recursiv参数

      • svn delete :删除文件或目录或符号链接,如果是文件夹的话,在你的本地项目拷贝里面不会立刻被删除,而是要等到你下一次提交的时候)

      • svn copy :顾名思义,拷贝东西svn copy source dest

      • svn move :顾名思义,移动东西,这里等同于拷贝后再删除

    • 检验修改(在提交之前检查一下你做过哪些修改,这些命令也对你本地工作目录进行)

      • svn status :查看修改

      • svn diff :查看修改的详细信息

    使用svn status能看到一些信息,比如:

    A stuff/loot/bloo.h # file is scheduled for addition
    C stuff/loot/lump.c # file has textual conflicts from an update
    D stuff/fish.c # file is scheduled for deletion
    M bar.c # the content in bar.c has local modifications

    ACDM分别代表addition conflict deletion modifications

    • 可能会取消一些修改

      • svn revert :取消修改,这个命令会把文件还原到上一次同步后的状态,当然也可以回复被你删掉的文件。

    • 解决冲突(合并别人的修改)

      • svn update :返回的结果前面的符号,U代表未改动,G代表已改动但没有重叠的地方,可以直接合并,C代表有冲突

      • svn resolved :你解决了冲突之后,使用这个命令通知服务器冲突已经解决。

    • 提交你的修改

      • svn commit