boost 学习笔记 2: timer

C++中操作时间的类。timer 是一个很小的库,提供简易的度量时间和进度显示的功能,可用于测试性能计时任务等大多数情况。timer 库包含三个组件, 计时器类 timer, progress_timer 和进度条指示类 progress_display。对应书中第二章内容。

timer

头文件

timer 位于boost命名空间,需要包含头文件 <boost/timer.hpp> 即:

#include <boost/timer.hpp>
using namespace boost;

基本方法

timer变量声明之后即开始计时工作,之后可以调用 elapsed() 方法来测量对象自创建之后所流逝的时间。成员函数 elapsed_max()elapsed_min() 方法返回 timer 能够测量的时间最大值和最小值,输出单位秒。timer类的实现代码很少,可参考源码学习。

/**
	 * timer simple and useful, suitable for short time task.
	 * Do not use it to calculate time for long time.
	 * For long time calculate check date_time lib
	 */
using boost::timer;
timer t;															// declare timer start counting
cout << "max timespan:" << t.elapsed_max() / 3600 << "h" << endl;
cout << "min timespan:" << t.elapsed_min() << "s" << endl;
cout << "now time elapsed:" << t.elapsed() << "s" << endl;

timer 实现源码

使用标准库头文件 中的 std::clock() 函数。

class timer
{
 public:
         timer() { _start_time = std::clock(); } // postcondition: elapsed()==0
//         timer( const timer& src );      // post: elapsed()==src.elapsed()
//        ~timer(){}
//  timer& operator=( const timer& src );  // post: elapsed()==src.elapsed()
  void   restart() { _start_time = std::clock(); } // post: elapsed()==0
  double elapsed() const                  // return elapsed time in seconds
    { return  double(std::clock() - _start_time) / CLOCKS_PER_SEC; }

  double elapsed_max() const   // return estimated maximum value for elapsed()
  // Portability warning: elapsed_max() may return too high a value on systems
  // where std::clock_t overflows or resets at surprising values.
  {
    return (double((std::numeric_limits<std::clock_t>::max)())
       - double(_start_time)) / double(CLOCKS_PER_SEC); 
  }

  double elapsed_min() const            // return minimum value for elapsed()
   { return double(1)/double(CLOCKS_PER_SEC); }

 private:
  std::clock_t _start_time;
}; // timer

使用建议

  • 不适合高精度计时,精度依赖操作系统或编译器
  • 不适合大跨度时间段测量,最大跨度只有几百小时,如需要天、月甚至年做时间单位,可使用 date_time 库

progress_timer

头文件

#include <boost/progress.hpp>
using namespace boost;

基本用法

在定义 progress_timer 之后,析构对象时会自动输出流逝时间。可以使用花括号,来使 progress_timer 实现计时功能。

progress_timer t;
// do some work
cout << t.elapsed() << endl;	//print out elapsed time for the task

progress_timer 精度问题,只能保留到小数点后两位,精确到百分之一秒。

progress_display

头文件

#include <boost/progress.hpp>
using namespace boost;

基本用法

在构造函数中传入总数,然后迭代更新进度条。

vector<string> v(100);
ofstream fs("test.txt");
progress_display pd(v.size());					// 构造函数,传入进度总数

vector<string>::iterator pos;
for (pos = v.begin(); pos != v.end(); ++pos){	// 遍历迭代,处理字符串,写文件
    fs << *pos << endl;
    ++pd;										// 更新进度
}

progress_display 问题

progress_display 向标准输出 cout 输出字符,无法将进度条和输出分离,如果循环中带有输出,则显示会很难看。


2015-12-22 boost , C++

FFmpeg 入门笔记

那天需要将一段视频文件转成 gif,偶遇 ffmpeg,于是就学习了一下,它真的很强大。在看资料的过程中也是挺有趣的,发现其实 kmplayer 以及国内的 QQ 影音,暴风等等,都不同程度的使用了 ffmpeg,可是根据 ffmpeg 的开源许可 LGPL,任何使用 ffmpeg 的软件都必须开源,于是乎 QQ 影音,暴风都上了 ffmpeg 的耻辱柱,如果没接触到 ffmpeg 还真不知道有这一茬,国内的黑心厂商真是拿开源社区的东西都不遵循开源协议。

下面就直接进正题吧:

几个概念

在进入 ffmpeg 入门之前有一些基本概念需要了解,我在查看 ffmpeg 的时候回头查阅了这些资料,觉得先行了解比较好,这些概念都是视频或者音频中的基本概念。

比特率

[[比特率]],英文为 bit rate,描述每秒钟输出多少 KB 的参数,单位是 Kbps,也就是 kbit/s,8Kbit/s = 1KB/s。也就是说 800Kbps 意思就是每秒视频就要占用 100KB 磁盘空间。对于音频文件也存在比特率,同理。压缩同一个视频,视频比特率越大,文件体积越大。视频比特率越大,画质越好,马赛克越少。

MP3 一般使用的比特率为 8~320kbps。

举一个例子:

bitrate 可以理解为 file size / duration

比如一个视频文件 20.8 M 时长为 1min,那么

bitrate = 20.8M bit/60s = 20.8 * 1024 * 1024 * 8 bit/60s = 2831 Kbps

假设音频的码率为固定 128 Kbps,那么视频的码率就是

2831 Kbps - 128 Kbps = 2703 Kbps

H.264 标准建议

视频大小 分辨率 建议码率
480P 720X480 1800Kbps
720P 1280X720 3500Kbps
1080P 1920X1080 8500Kbps

可变码率

可变码率叫做 Variable Bitrate (VBR),也叫作动态比特率编码,VBR 指的是编码器的输出码率可以根据编码器输入源信号的复杂度自适应调整,目的是为了达到输出质量保持不变的同时节省存储空间。VBR 适用于存储,不太适用流式传输,可以更有效的地利用有限空间。

固定码率

固定码率叫做 Constant Bitrate (CBR),CBR 指的是编码器输出码率固定,CBR 不适合存储,CBR 对于复杂内容可能没有足够码率进行编码,从而导致质量下降,同时会在简单内容部分浪费一些码率。

帧数

帧数,又被叫做帧率,指的是每秒钟播放的图片数,单位 fps(英文:Frames Per Second),每秒的帧数或者帧率表示视频文件或者图形处理器场景时每秒钟能够更新的次数。

高的帧率可以得到更流畅、更逼真的画面。一般来说 30fps 就是可以接受的,但是将性能提升至 60fps 则可以明显提升交互感和逼真感,但是一般来说超过 75fps 一般就不容易察觉到有明显的流畅度提升了。如果帧率超过屏幕刷新率只会浪费图形处理的能力,因为显示器不能以这么快的速度更新,这样超过刷新率的帧率就浪费掉了。

在同一视频,同一码率的情况下,帧数越大,则画质越不好。尤其是运动的画面。因为每张画面会分担每秒有限的文件体积,如果画面越多,那么每张画面所能表现的内容就越有限。

当画面的 FPS 达到 60 帧 / 秒时,已经能满足绝大部分应用需求。一般情况下,如果能够保证游戏画面的平均 FPS 能够达到 30 帧 / 秒,那么画面已经基本流畅;能够达到 50 帧 / 秒,就基本可以体会到行云流水的感觉了。一般人很难分辨出 60 帧 / 秒与 100 帧 / 秒有什么不同。

分辨率

[[分辨率]] 是最好理解的概念了,因为日常就能看到很多地方使用,表示画面的大小,单位是像素 px。

和编码率的关系:越高的分辨率,需要越高的编码率,因为图像的细节多了,需要的文件体积也应该增大,否则还不如画面小一些,你会发现同一码率,画面越大,图像的马赛克程度越明显。

采样率

[[采样率]] 是指每秒钟对音频信号的采样次数,采样频率越高声音还原度越高,声音更加自然。单位是赫兹 Hz。音频文件一般使用的采样率是 44100 Hz ,也就是一秒钟采样 44100 次,之所以使用这个数值是因为经过了反复实验,人们发现这个采样精度最合适,低于这个值就会有较明显的损失,而高于这个值人的耳朵已经很难分辨,而且增大了数字音频所占用的空间。我们所使用的 CD 的采样标准就是 44.1k,目前 44.1k 还是一个最通行的标准。

安装

Debian/Ubuntu/Linux Mint 下安装 ffmpeg 很简单:

apt-get install ffmpeg

其他操作系统安装方法,参考官网

如果想要手工编译 ffmpeg 可以参考官方 wiki。 Ubuntu/Debian/Mint 系手工编译 ffmpeg 参考 wiki

用法举例

显示文件信息

显示视频信息

ffmpeg -i input.avi

将视频拆分图片 批量截图

将视频拆分多张图片,每一帧图片,保存到 frames 文件夹下,命名 frame001.png 这种。可以加上 -r 参数以用来限制每秒的帧数,-r 10 就表示每秒 10 帧。

ffmpeg -i input.mp4 frames/frame%03d.png

图片合成视频

将多张图片合成视频

ffmpeg -i frames/frame%3d.png output.mp4

从视频中提取音频

从视频文件中提取音频并保存为 mp3

ffmpeg -i input.mp4 -f mp3 output.mp3
# or
ffmpeg -i input.mp4 -vn output.mp3
# or
ffmpeg -i input.mp4 -vn -ar 44100 -ac 2 -ab 320 -f mp3 output.mp3

说明:

如果需要可以在中间加上 -ar 44100 -ac 2 -ab 192 系数,表示采样率 44100 ,通道 2 立体声,码率 192 kb/s.

  • -vn 表示不使用视频
  • -ar 设置音频采样率
  • -ac 设置音频 Channels
  • -ab 设置音频比特率 bitrate
  • -f 指定输出文件的格式

将声音合成到视频

将声音合成到视频中

ffmpeg -i input_music.mp3 -i input_video.mp4 output.mp4

将音频流和视频流合并:

ffmpeg -i input.mp4 -i input.mp3 -c copy -map 0:v:0 -map 1:a:0 output.mp4
  • -map 参数指定了从输入文件的视频流和音频流中输出文件
  • 如果你的音频流时长超过视频流,或者相反,你可以使用-shortest参数,使ffmpeg参数在处理到两个输入短的结束时结束。

转化格式

格式之间转换 大部分的情况下直接运行一下即可

ffmpeg -i input.mp4 output.avi

将 flv 转码 MP4

ffmpeg -i input.flv -vcodec copy -acodec copy out.mp4

-vcodec copy-acodec copy 表示所使用的视频和音频编码格式,为原样拷贝。

转换文件格式

ffmpeg -y -i input_video.mp4 -bitexact -vcodec h263 -b 128 -r 15 -s 176x144 -acodec aac -ac 2 -ar 22500 -ab 24 -f 3gp test.3gp

ffmpeg -y -i test.wmv -ac 1 -acodec libamr_nb -ar 8000 -ab 12200 -s 176x144 -b 128 -r 15 test.3gp

视频切片操作

对视频切片操作

比如需要从视频第 1 分 45 秒地方,剪 10 秒画面,-ss 表示开始位置,-t 表示延长时间

ffmpeg -i input.mp4 -ss 00:01:45 -t 10 output.mp4

如果要精确控制切片的时间,比如想要剪切从 0 秒到 10 分的视频1

ffmpeg -ss 00:00:00 -to 00:10:00 -i input.mp4 -c copy output.mp4

加速减速视频

加速视频

ffmpeg -i input.mp4 -vf “setpts=0.5*PTS” output.mp4

同理减速视频

ffmpeg -i input.mp4 -vf “setpts=2.0*PTS” output.mp4

此操作对音频无影响

视频截图

视频 10 秒的地方 (-ss 参数)截取一张 1920x1080 尺寸大小的,格式为 jpg 的图片 -ss后跟的时间单位为秒

ffmpeg -i input_video.mp4 -y -f image2 -t 0.001 -ss 10 -s 1920x1080 output.jpg

或者

ffmpeg -i input_video.mp4 -ss 00:00:06.000 -vframes 1 output.png

合成 gif

把视频的前 30 帧转换成一个 Gif

ffmpeg -i input_video.mp4 -vframes 30 -y -f gif output.gif

将视频转成 gif

ffmpeg -ss 00:00:00.000 -i input.mp4 -pix_fmt rgb24 -r 10 -s 320x240 -t 00:00:10.000 output.gif

将输入的文件从 (-ss) 设定的时间开始以 10 帧频率,输出到 320x240 大小的 gif 中,时间长度为 -t 设定的参数。通过这样转换出来的 gif 一般都比较大,可以使用 ImageMagick 来优化图片的大小。

 convert -layers Optimize output.gif output_optimized.gif

把 frame.[001-100].jpg 序列帧和 bg.mp3 音频文件利用 mpeg4 编码方式合成分辨率 720p 的视频文件 output.avi:

ffmpeg -i bg.mp3 -i frame.%3d.jpg -s hd720 -vcodec mpeg4 output.avi

转换码率

ffmpeg 码率相关的参数主要有 -minrate, maxrate, -b:v

ffmpeg -i input.mp4 -b:v 2000k output.mp4

也就是把原始视频转换成 2 Mbps 码率视频。ffmpeg 官方建议,在设置 -b:v 时,同时加上 -bufsize 用于设置码率控制缓冲器大小,让整体码率更加趋近于希望的值,减少波动。

ffmpeg -i input.mp4 -b:v 2000k -bufsize 2000k output.mp4

-minrate-maxrate 比较简单,设置码率不要低于或者超过某一个阈值

ffmpeg -i input.mp4 -b:v 2000k -bufsize 2000k -maxrate 2500k output.mp4

强制输出文件的帧率

比如要强制输出文件的帧率为 24 fps:

ffmpeg -i input.avi -r 24 output.avi

压缩视频大小

对于一个非常大的文件,经常需要压缩文件大小可以使用

 ffmpeg -i input.mp4 -vcodec h264 -acodec mp3 output.mp4

更改视频的分辨率

如果想要改变视频的分辨率

ffmpeg -i input.mp4 -filter:v scale=1280:720 -c:a copy output.mp4
# or
ffmpeg -i input.mp4 -s 1280x720 -c:a copy output.mp4

裁剪视频

ffmpeg -i input.mp4 -filter:v "crop=w:h:x:y" output.mp4

说明:

  • -filter:v 过滤视频

设置视频的宽高比

ffmpeg -i input.mp4 -aspect 16:9 output.mp4

常见的宽高比:

  • 16:9
  • 4:3
  • 16:10
  • 5:4

给音频文件增加图片

将一个音频文件编程一个视频

ffmpeg -loop 1 -i input_image.jpg -i input_audio.mp3 -c:v libx264 -c:a aac -strict experimental -b:a 192k -shortest output.mp4

给视频文件增加字幕

ffmpeg -i ./file.mp4 -c:v libx264 -c:a copy -vf "ass=t.ass" out.mp4

查看 ffmpeg 支持格式

要查看你的 ffmpeg 支持哪些格式,可以用如下命令:

ffmpeg -formats | less

设置输出文件编码率 64 kbit/s, To set the video bitrate of the output file to 64 kbit/s:

 ffmpeg -i input.avi -b:v 64k -bufsize 64k output.avi

设置输出文件帧率为 24 fps,To force the frame rate of the output file to 24 fps:

 ffmpeg -i input.avi -r 24 output.avi

强制输入文件以 1 帧,输出文件 24 帧 , To force the frame rate of the input file (valid for raw formats only) to 1 fps and the frame rate of the output file to 24 fps:

 ffmpeg -r 1 -i input.mp4 -r 24 output.avi

下面几步分别是,创建frames文件夹,利用 ffmpeg 将视频文件以每秒 10 帧输出成图像保存到 frames 文件夹中,再利用 ImageMagick 将图片组成 gif。其中 convert 命令来自 ImageMagick。

mkdir frames
ffmpeg -i input.mp4 -r 10 frames/frame%03d.png
convert -delay 5 -loop 0 frames/frame*.png output.gif

Source: http://superuser.com/a/556031

利用 ffmpeg 屏幕录制

参考:https://trac.ffmpeg.org/wiki/Capture/Desktop

添加水印

ffmpeg -i input.mp4 -i picture.png -filter_complex overlay="(main_w/2)-(overlay_w/2):(main_h/2)-(overlay_h)/2" output.mp4

picture.png 为水印图片, overlay 为水印位置

ffmpeg 使用语法

ffmpeg 使用语法:

ffmpeg [global_options] {[input_file_options] -i input_file} ... {[output_file_options] output_file} ...

如果没有输入文件,那么视音频捕捉就会起作用。

作为通用的规则,选项一般用于下一个特定的文件。如果你给 –b 64 选项,改选会设置下一个视频速率。对于原始输入文件,格式选项可能是需要的。

缺省情况下,ffmpeg 试图尽可能的无损转换,采用与输入同样的音频视频参数来输出。

通用选项

-L license 显示协议
-h 帮助
-formats 显示可用的格式,编解码的,协议的
-decoders 可用解码器
-encoders 可用编码器

主要选项

-i 	filename 输入文件
-y 	覆盖输出文件
-n 	不覆盖输出文件,如果输出文件存在则退出
-t 	duration (input/output)
    设置纪录时间 hh:mm:ss[.xxx] 格式的记录时间也支持,在 `-i` 之前使用,则对输入文件限制记录时间;如果对输出文件使用,则是限制输出文件的时长。

-ss position
    搜索到指定的时间 [-]hh:mm:ss[.xxx] 的格式也支持 ,更多[参考](https://ffmpeg.org/ffmpeg-utils.html#time-duration-syntax)

-title string 设置标题
-author string 设置作者
-copyright string 设置版权
-comment string 设置评论
-f fmt 强迫采用格式 fmt 输出

-c[:stream_specifier] codec (input/output, per-stream)
-codec[:stream_specifier] codec (input/output, per-stream)
    给输入文件指定解码器,给输出文件指定编码器, codec 为编码器名字,如果 codec 值为 `copy` 则默认为和原视频一致。

-vcodec codec
    vcodec 是 -codec:v 的一个别称,强制使用 codec 编解码方式,未设定时使用与输入流相同的编码器。如果用 copy 表示原始编解码数据必须被拷贝。

-target type 设置目标文件类型 (vcd,svcd,dvd) 所有的格式选项(比特率,编解码以及缓冲区大小)自动设置,只需要输入如下的就可以了:

    ffmpeg -i input.avi -target vcd /tmp/vcd.mpg

-hq 激活高质量设置
-itsoffset offset 设置以秒为基准的时间偏移,该选项影响所有后面的输入文件。该偏移被加到输入文件的时戳,定义一个正偏移意味着相应的流被延迟了 offset 秒。 [-]hh:mm:ss[.xxx] 的格式也支持

### 视频选项 {#video-options}

-vframes number (output)
    设置视频输出帧数,是`-frames:v`的别称。

-b bitrate 设置比特率,缺省 200kb/s
-r fps 设置帧率 缺省 25
-s size 设置画面的宽高

    设置帧大小,分辨率, 格式为 wxh 缺省为原视频大小。下面的简写也可以直接使用:
    ntsc 720x480
    snits 640x480
    hd720 1280x720
    hd1080 1920x1080
    更多[参考](https://ffmpeg.org/ffmpeg-utils.html#toc-Video-size)

-aspect aspect 设置画面比例 4:3 16:9 或 1.3333 1.7777
-croptop size 设置顶部切除带大小 像素单位
-cropbottom size –cropleft size –cropright size
-padtop size 设置顶部补齐的大小 像素单位

-padbottom size –padleft size –padright size –padcolor color 设置补齐条颜色 (hex,6 个 16 进制的数,红:绿:兰排列,比如 000000 代表黑色)

-vn 不做视频记录,输出无视频内容
-bt tolerance 设置视频码率容忍度 kbit/s
-maxrate bitrate 设置最大视频码率容忍度
-minrate bitreate 设置最小视频码率容忍度

-bufsize size 设置码率控制缓冲区大小

-sameq 使用同样视频质量作为源(VBR)

-pass n 选择处理遍数(1 或者 2)。两遍编码非常有用。第一遍生成统计信息,第二遍生成精确的请求的码率

-passlogfile file 选择两遍的纪录文件名为 file

高级视频选项

-g gop_size 设置图像组大小

-intra 仅适用帧内编码

-qscale q 使用固定的视频量化标度 (VBR)

-qmin q 最小视频量化标度 (VBR)

-qmax q 最大视频量化标度 (VBR)

-qdiff q 量化标度间最大偏差 (VBR)

-qblur blur 视频量化标度柔化 (VBR)

-qcomp compression 视频量化标度压缩 (VBR)

-rc_init_cplx complexity 一遍编码的初始复杂度

-b_qfactor factor 在 p 和 b 帧间的 qp 因子

-i_qfactor factor 在 p 和 i 帧间的 qp 因子

-b_qoffset offset 在 p 和 b 帧间的 qp 偏差

-i_qoffset offset 在 p 和 i 帧间的 qp 偏差

-rc_eq equation 设置码率控制方程 默认 tex^qComp

-rc_override override 特定间隔下的速率控制重载

-me method 设置运动估计的方法 可用方法有 zero phods log x1 epzs(缺省) full

-dct_algo algo 设置 dct 的算法 可用的有 0 FF_DCT_AUTO 缺省的 DCT 1 FF_DCT_FASTINT 2 FF_DCT_INT 3 FF_DCT_MMX 4 FF_DCT_MLIB 5 FF_DCT_ALTIVEC

-idct_algo algo 设置 idct 算法。可用的有 0 FF_IDCT_AUTO 缺省的 IDCT 1 FF_IDCT_INT 2 FF_IDCT_SIMPLE 3 FF_IDCT_SIMPLEMMX 4 FF_IDCT_LIBMPEG2MMX 5 FF_IDCT_PS2 6 FF_IDCT_MLIB 7 FF_IDCT_ARM 8 FF_IDCT_ALTIVEC 9 FF_IDCT_SH4 10 FF_IDCT_SIMPLEARM

-er n 设置错误残留为 n 1 FF_ER_CAREFULL 缺省 2 FF_ER_COMPLIANT 3 FF_ER_AGGRESSIVE 4 FF_ER_VERY_AGGRESSIVE

-ec bit_mask 设置错误掩蔽为 bit_mask, 该值为如下值的位掩码 1 FF_EC_GUESS_MVS (default=enabled) 2 FF_EC_DEBLOCK (default=enabled)

-bf frames 使用 frames B 帧,支持 mpeg1,mpeg2,mpeg4

-mbd mode 宏块决策 0 FF_MB_DECISION_SIMPLE 使用 mb_cmp 1 FF_MB_DECISION_BITS 2 FF_MB_DECISION_RD

-4mv 使用 4 个运动矢量 仅用于 mpeg4

-part 使用数据划分 仅用于 mpeg4

-bug param 绕过没有被自动监测到编码器的问题

-strict strictness 跟标准的严格性

-aic 使能高级帧内编码 h263+

-umv 使能无限运动矢量 h263+

-deinterlace 不采用交织方法

-interlace 强迫交织法编码仅对 mpeg2 和 mpeg4 有效。当你的输入是交织的并且你想要保持交织以最小图像损失的时候采用该选项。可选的方法是不交织,但是损失更大

-psnr 计算压缩帧的 psnr

-vstats 输出视频编码统计到 vstats_hhmmss.log

-vhook module 插入视频处理模块 module 包括了模块名和参数,用空格分开

### 音频选项 {#audio-options}

-aframes number (output)
    设置输出文件音频帧数,是`-frames:a` 的别名

-ab bitrate
    设置音频码率,声音比特率,-ac 设为立体声时要以一半比特率来设置,比如 192kbps 的就设置成 96,高品质音乐建议 160kbps(80) 一上

-ar freq
    设置音频采样率,一般设置 44100

-ac channels
    设置通道,声道数, 缺省为 1, 1 为单声道,2 为立体声

-an 不使用音频纪录

-acodec codec
    使用 codec 编解码,是`-codec:a`的别名

音频 / 视频捕获选项

-vd device 设置视频捕获设备。比如 /dev/video0

-vc channel 设置视频捕获通道 DV1394 专用

-tvstd standard 设置电视标准 NTSC PAL(SECAM)

-dv1394 设置 DV1394 捕获

-av device 设置音频设备 比如 /dev/dsp

### 高级选项 {#advance-option}

-map file:stream 设置输入流映射

-debug 打印特定调试信息

-benchmark 为基准测试加入时间

-hex 倾倒每一个输入包

-bitexact 仅使用位精确算法 用于编解码测试

-ps size 设置包大小,以 bits 为单位

-re 以本地帧频读数据,主要用于模拟捕获设备

-loop 循环输入流。只工作于图像流,用于 ffserver 测试

附录 1:ffmpeg 简略帮助

Hyper fast Audio and Video encoder
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...

Getting help:
    -h      -- print basic options
    -h long -- print more options
    -h full -- print all options (including all format and codec specific options, very long)
    -h type=name -- print all options for the named decoder/encoder/demuxer/muxer/filter
    See man ffmpeg for detailed description of the options.

Print help / information / capabilities:
-L                  show license
-h topic            show help
-? topic            show help
-help topic         show help
--help topic        show help
-version            show version
-buildconf          show build configuration
-formats            show available formats
-devices            show available devices
-codecs             show available codecs
-decoders           show available decoders
-encoders           show available encoders
-bsfs               show available bit stream filters
-protocols          show available protocols
-filters            show available filters
-pix_fmts           show available pixel formats
-layouts            show standard channel layouts
-sample_fmts        show available audio sample formats
-colors             show available color names
-sources device     list sources of the input device
-sinks device       list sinks of the output device
-hwaccels           show available HW acceleration methods

Global options (affect whole program instead of just one file:
-loglevel loglevel  set logging level
-v loglevel         set logging level
-report             generate a report
-max_alloc bytes    set maximum size of a single allocated block
-y                  overwrite output files
-n                  never overwrite output files
-ignore_unknown     Ignore unknown stream types
-stats              print progress report during encoding
-max_error_rate ratio of errors (0.0: no errors, 1.0: 100% error  maximum error rate
-bits_per_raw_sample number  set the number of bits per raw sample
-vol volume         change audio volume (256=normal)

Per-file main options:
-f fmt              force format
-c codec            codec name
-codec codec        codec name
-pre preset         preset name
-map_metadata outfile[,metadata]:infile[,metadata]  set metadata information of outfile from infile
-t duration         record or transcode "duration" seconds of audio/video
-to time_stop       record or transcode stop time
-fs limit_size      set the limit file size in bytes
-ss time_off        set the start time offset
-sseof time_off     set the start time offset relative to EOF
-seek_timestamp     enable/disable seeking by timestamp with -ss
-timestamp time     set the recording timestamp ('now' to set the current time)
-metadata string=string  add metadata
-program title=string:st=number...  add program with specified streams
-target type        specify target file type ("vcd", "svcd", "dvd", "dv" or "dv50" with optional prefixes "pal-", "ntsc-" or "film-")
-apad               audio pad
-frames number      set the number of frames to output
-filter filter_graph  set stream filtergraph
-filter_script filename  read stream filtergraph description from a file
-reinit_filter      reinit filtergraph on input parameter changes
-discard            discard
-disposition        disposition

Video options:
-vframes number     set the number of video frames to output
-r rate             set frame rate (Hz value, fraction or abbreviation)
-s size             set frame size (WxH or abbreviation)
-aspect aspect      set aspect ratio (4:3, 16:9 or 1.3333, 1.7777)
-bits_per_raw_sample number  set the number of bits per raw sample
-vn                 disable video
-vcodec codec       force video codec ('copy' to copy stream)
-timecode hh:mm:ss[:;.]ff  set initial TimeCode value.
-pass n             select the pass number (1 to 3)
-vf filter_graph    set video filters
-ab bitrate         audio bitrate (please use -b:a)
-b bitrate          video bitrate (please use -b:v)
-dn                 disable data

Audio options:
-aframes number     set the number of audio frames to output
-aq quality         set audio quality (codec-specific)
-ar rate            set audio sampling rate (in Hz)
-ac channels        set number of audio channels
-an                 disable audio
-acodec codec       force audio codec ('copy' to copy stream)
-vol volume         change audio volume (256=normal)
-af filter_graph    set audio filters

Subtitle options:
-s size             set frame size (WxH or abbreviation)
-sn                 disable subtitle
-scodec codec       force subtitle codec ('copy' to copy stream)
-stag fourcc/tag    force subtitle tag/fourcc
-fix_sub_duration   fix subtitles duration
-canvas_size size   set canvas size (WxH or abbreviation)
-spre preset        set the subtitle options to the indicated preset

附录 2:常用视频文件格式详解

常见的视频格式:

  1. AVI 格式   它的英文全称为 Audio Video Interleaved,即音频视频交错格式。它于 1992 年被 Microsoft 公司推出,随 Windows3.1 一起被人们所认识和熟知。所谓“音频视频交错”,就是可以将视频和音频交织在一起进行同步播放。这种视频格式的优点是图像质量好,可以跨多个平台使用,但是其缺点是体积过于庞大,而且更加糟糕的是压缩标准不统一,因此经常会遇到高版本 Windows 媒体播放器播放不了采用早期编码编辑的 AVI 格式视频,而低版本 Windows 媒体播放器又播放不了采用最新编码编辑的 AVI 格式视频。其实解决的方法也非常简单,我们将在后面的视频转换、视频修复部分中给出解决的方案。

  2. DV-AVI 格式   DV 的英文全称是 Digital Video Format,是由索尼、松下、JVC 等多家厂商联合提出的一种家用数字视频格式。目前非常流行的数码摄像机就是使用这种格式记录视频数据的。它可以通过电脑的 IEEE 1394 端口传输视频数据到电脑,也可以将电脑中编辑好的的视频数据回录到数码摄像机中。这种视频格式的文件扩展名一般也是.avi,所以我们习惯地叫它为 DV-AVI 格式。

  3. MPEG 格式   它的英文全称为 Moving Picture Expert Group,即运动图像专家组格式,家里常看的 VCD、SVCD、DVD 就是这种格式。MPEG 文件格式是运动图像压缩算法的国际标准,它采用了有损压缩方法从而减少运动图像中的冗余信息。MPEG 的压缩方法说的更加深入一点就是保留相邻两幅画面绝大多数相同的部分,而把后续图像中和前面图像有冗余的部分去除,从而达到压缩的目的。目前 MPEG 格式有三个压缩标准,分别是 MPEG-1、MPEG-2、和 MPEG-4,另外,MPEG-7 与 MPEG-21 仍处在研发阶段。   MPEG-1:制定于 1992 年,它是针对 1.5Mbps 以下数据传输率的数字存储媒体运动图像及其伴音编码而设计的国际标准。也就是我们通常所见到的 VCD 制作格式。这种视频格式的文件扩展名包括.mpg、.mlv、.mpe、.mpeg 及 VCD 光盘中的.dat 文件等。   MPEG-2:制定于 1994 年,设计目标为高级工业标准的图像质量以及更高的传输率。这种格式主要应用在 DVD/SVCD 的制作(压缩)方面,同时在一些 HDTV(高清晰电视广播)和一些高要求视频编辑、处理上面也有相当的应用。这种视频格式的文件扩展名包括.mpg、.mpe、.mpeg、.m2v 及 DVD 光盘上的.vob 文件等。   MPEG-4:制定于 1998 年,MPEG-4 是为了播放流式媒体的高质量视频而专门设计的,它可利用很窄的带度,通过帧重建技术,压缩和传输数据,以求使用最少的数据获得最佳的图像质量。MPEG-4 最有吸引力的地方在于它能够保存接近于 DVD 画质的小体积视频文件。这种视频格式的文件扩展名包括.asf、.mov 和 DivX 、AVI 等。

  4. DivX 格式   这是由 MPEG-4 衍生出的另一种视频编码(压缩)标准,也即我们通常所说的 DVDrip 格式,它采用了 MPEG4 的压缩算法同时又综合了 MPEG-4 与 MP3 各方面的技术,说白了就是使用 DivX 压缩技术对 DVD 盘片的视频图像进行高质量压缩,同时用 MP3 或 AC3 对音频进行压缩,然后再将视频与音频合成并加上相应的外挂字幕文件而形成的视频格式。其画质直逼 DVD 并且体积只有 DVD 的数分之一。

  5. MOV 格式   美国 Apple 公司开发的一种视频格式,默认的播放器是苹果的 QuickTimePlayer。具有较高的压缩比率和较完美的视频清晰度等特点,但是其最大的特点还是跨平台性,即不仅能支持 MacOS,同样也能支持 Windows 系列。

  6. ASF 格式   它的英文全称为 Advanced Streaming format,它是微软为了和现在的 Real Player 竞争而推出的一种视频格式,用户可以直接使用 Windows 自带的 Windows Media Player 对其进行播放。由于它使用了 MPEG-4 的压缩算法,所以压缩率和图像的质量都很不错。

  7. WMF 格式   它的英文全称为 Windows Media Video,也是微软推出的一种采用独立编码方式并且可以直接在网上实时观看视频节目的文件压缩格式。WMV 格式的主要优点包括:本地或网络回放、可扩充的媒体类型、可伸缩的媒体类型、多语言支持、环境独立性、丰富的流间关系以及扩展性等。

  8. RM 格式   Networks 公司所制定的音频视频压缩规范称之为 Real Media,用户可以使用 RealPlayer 或 RealOne Player 对符合 RealMedia 技术规范的网络音频 / 视频资源进行实况转播,并且 RealMedia 还可以根据不同的网络传输速率制定出不同的压缩比率,从而实现在低速率的网络上进行影像数据实时传送和播放。这种格式的另一个特点是用户使用 RealPlayer 或 RealOne Player 播放器可以在不下载音频 / 视频内容的条件下实现在线播放。

  9. RMVB 格式   这是一种由 RM 视频格式升级延伸出的新视频格式,它的先进之处在于 RMVB 视频格式打破了原先 RM 格式那种平均压缩采样的方式,在保证平均压缩比的基础上合理利用比特率资源,就是说静止和动作场面少的画面场景采用较低的编码速率,这样可以留出更多的带宽空间,而这些带宽会在出现快速运动的画面场景时被利用。这样在保证了静止画面质量的前提下,大幅地提高了运动图像的画面质量,从而图像质量和文件大小之间就达到了微妙的平衡。

参考


2015-12-21 ffmpeg , linux , media , movie , mp4 , mp3 , format , ffplay , ffprobe

boost 学习笔记 1: lexical_cast

开始接触boost是因为项目中用到C++与Python的相互调用传值,后来找到一本《boost程序库完全开发指南》感觉boost库很强大,就学了一下。所以boost学习笔记基本沿用《boost程序库完全开发指南》书中脉络。

因为C++是强类型语言,所以对于Python,perl之类的动态语言来说很麻烦的一件事情就是类型转换,虽然C中也提供了atoi(),atof()之类的函数,但是总体也还是很麻烦。幸而有了lexical_cast,他可以用来进行字符串、整数/浮点数之间的字面转换。

头文件

lexical_cast 位于boost命名空间,为了使用 lexical_cast,需要包含头文件 <boost/lexical_cast.hpp>,即:

#include <boost/lexical_cast.hpp>
using namespace boost;

基本方法

lexical_cast在转换字符串时,字符串中只能包含数字和小数点,不能出现除e/E以外的英文字符或者其他非数字字符。

using boost::lexical_cast;
int a = lexical_cast<int>("123");					// string -> int
long b = lexical_cast<long>("2015");				// string -> long
double c = lexical_cast<double>("123.12");			// string -> double
float pai = lexical_cast<float>("3.14159");			// string -> float

std::cout << a << b << c << pai << std::endl;

数字转换成字符串时不支持高级格式控制。

string str = lexical_cast<string>(123);				// int -> string
std::cout << str << std::endl;
cout << lexical_cast<string>(1.234) << endl;		// float -> string
cout << lexical_cast<string>(0x11) << endl;			// 16进制 -> string

// lexical_cast can cast 0 & 1 to bool, but only support 0 & 1, not support True or False string
bool bl = lexical_cast<bool>("1");					// string -> bool, only support 1 & 0

异常操作

当 lexical_cast 无法执行转换时会抛出异常 bad_lexical_cast ,它是 std::bad_cast 的派生类。可以使用 try/catch 块来保护代码。

try{
	cout << lexical_cast<int>("0x100");
	cout << lexical_cast<bool>("false");
}catch (bad_lexical_cast& e){
	cout << "error: " << e.what() << endl;
}

代码运行结果:

error: bad lexical cast: source type value could not be interpreted as target

转换对象要求

lexical_cast 对转换对象有如下要求:

  1. 转换起点对象是可流输出的,即定义了 operator«
  2. 转换终点对象是可流输入的,即定义了 operator»
  3. 转换终点对象必须是可缺省构造和可拷贝构造的

C++中内建类型(int,double等)和std::string 都是符合三个条件的。

自定义类

如果想要将 lexical_cast 用于自定义类,实现 java 中 Object.toString() 的用法,只需要满足 lexical_cast 的要求,实现流输出操作符 operator« 即可。

class demo_class{
    friend ostream& operator<<(ostream& os, const demo_class& x){
        os << "class content" << endl;
        return os;
    }
};
int main() {
    demo_class demo = demo_class();
    cout << lexical_cast<string>(demo) << endl;
}

输出结果:

class content


2015-12-21 boost , C++

boost 学习笔记 0: 安装环境

这篇文章讲如何安装 boost 库,最完整的教程永远在官网。以下内容部分翻译自官方文档。

首先来看一看在Linux下安装 boost 库。

Ubuntu/Debian/Linux Mint 安装boost

运行以下命令:

sudo apt-get install libboost-all-dev

然后输入密码,安装,安装完之后目录在 /usr/include/boost 下。

Linux下使用

Eclipse或者其他IDE中使用 boost 时,需要以下几步,引入头文件,添加库。

C/C++ Build, Cross G++ Linker,Libraries, 添加相应Libraries(-l),并添加相应Library search path(-L) /usr/include

boost config

Mac

Max 下安装 boost 最简单的方式,就是用 brew:

brew install boost

或者手动安装,参考boost官网

简单翻译:

  1. 下载 boost_1_60_0.tar.bz2
  2. 解压 tar –bzip2 -xf /path/to/boost_1_60_0.tar.bz2
  3. 一部分组件(Header-Only Libraries)在完成1和2以后就能直接用,因为是直接写在hpp的inline函数,但是要利用其它功能,需要build boost库里面的各个组件(步骤4-6)

    需要单独编译的库有:

    • Boost.Chrono
    • Boost.Context
    • Boost.Filesystem
    • Boost.GraphParallel
    • Boost.IOStreams
    • Boost.Locale
    • Boost.MPI
    • Boost.ProgramOptions
    • Boost.Python (see the Boost.Python build documentation before building and installing it)
    • Boost.Regex
    • Boost.Serialization
    • Boost.Signals
    • Boost.System
    • Boost.Thread
    • Boost.Timer
    • Boost.Wave
  4. 进入解压后的目录 cd path/to/boost_1_60_0
  5. 输入./bootstrap.sh 开始配置,添加 --prefix 选择安装地址 ./bootstrap.sh –prefix=path/to/installation/prefix
  6. 输入 ./b2 install 开始安装

备注:如果第5步直接输入./bootstrap.sh 则默认会安装到/usr/local下面的include和lib目录下,而/usr是在Macintosh HD下面的一个隐藏目录,到此boost就安装到了电脑上,可以使用它进行编程了。

Mac下默认安装地址是在 /usr/local/include/usr/local/lib 下,因此在配置环境的时候需要注意将boost地址写入。

xcode配置boost环境

基本思路和Linux中一样,添加头文件搜索路径,添加lib搜索路径,引用相应lib文件。

  1. 用xcode创建控制台应用程序,在项目设置->build Settings->Search Paths->Header Search Paths和Library Search Paths里面分别添加上述默认安装地址 /usr/local/include//usr/local/lib 目录
  2. 在项目设置->build Phases->Link Library With Libraries里面点加号,选择option,找到上述lib目录,选中里面以.a 或者 .dylib 结尾的文件,添加即可

参考:YouTube

其他操作系统请参考以上 YouTube Playlist,我收集整理了一些基本够用了。


2015-12-20 boost , C++

购买 VPS 之后需要做的事情

Security

Security is the most important thing we should take care at first.

Change password

The first thing you login into your VPS using root is to change your root password your VPS provider gave. Run the passwd to change your root password.

After you run this command, your terminal will prompt you to input new password. So just type your new password twice. Linux will check your new password to prevent simple password or short password. So don’t use any exist words or any password only contains number.

Create a new User

One of the most important security thing is try your best not to login to your VPS using root account. A better way is to create a new user account and do anything you like as this new user.

Following command is to create a new user and set a password for this user. Please replace einverne as your own name.

# create a new user called einverne
adduser einverne
# set password for user einverne
passwd einverne

After you create a new user account successfully, we give the new user root privileges. Someone may be curious about why we create a new user and grant root privileges, so why don’t we just use root account. There are two points. One is that this can prevent user making system-destroying mistakes, second is that all command run by sudo will have a record in /var/log/secure which can be reviewed later if needed.

update default editor

apt install vim -y
update-alternatives --config editor

choose: vim

sudo

Run visudo command to enter sudo config file. Find a section called user privilege specification. And add a new line under this section like this:

# User privilege specification
root    ALL=(ALL)       ALL
# new add
einverne	ALL=(ALL)	NOPASSWD:ALL

ssh configuration

Now it’s time to make the server more secure. You can set the ssh configuration to permit root login. But before doing this config, please make sure to have a new account have the root privileges.

Edit ssh config file:

 sudo vim /etc/ssh/sshd_config

Then change follow line:

Port 22
PermitRootLogin no

Port means the ssh port you can connect, you can set any number between 1025 and 65535. PermitRootLogin means you can disallow root login, if you set to no.

Finally, add AllowUsers einverne at the bottom of the sshd_config file.

Then reload the config file to make ssh work.

service ssh reload

To test the new ssh config, do not logout of root. Open a new terminal and login with your new account like:

ssh -p port einverne@server.address

servers

After you set up server ssh, you can generate SSH key at local computer and use SSH key to connect to server rather than using password. Generating a key at local computer:

ssh-keygen -t ed25519 -C "your@email.com"

follow the instruction of this command, for example you name it vps , just enter to skip password of key, and you will get two files under ~/.ssh/, vps is your private key, keep it safe. And vps.pub is the public key. And Now use ssh-copy-id to copy public key to server.

ssh-copy-id user@server.address

Then type the password. And it will be the last time to type your password to connect to server. If your computer don’t have the command ssh-copy-id, you have to copy the public key to server ~/.ssh/authorized_keys manually.

scp ~/.ssh/name.pub user@server:~/.ssh/

Then copy the file content to authorized_keys file.

cat name.pub >> authorized_keys

Finally to check the permission of the folder .ssh and file authorized_keys

drwx------ 2 einverne einverne       4096 Apr 19 21:25 .ssh
-rw------- 1 einverne einverne  744 Apr 19 21:14 authorized_keys

and if not:

chmod 700 ~/.ssh/
chmod 600 authorized_keys

setup alias

Add alias to .bashrc or .zshrc file.

alias vps = "ssh username@server -p port"

Then next time, you can just type vps to connect to server.

ssh config

There are two config file to setup ssh. One is system wide configuration file which can be found /etc/ssh/ssh_config. And another is per-user configuration file which is located under user home directory ~/.ssh/config. Most time we only care about user config.

Try to set up:

Host ds #this can be anything just a alias
	HostName server
	Port 22
	User username

Then we can use ssh ds to connect to server. If you have multi config just add to following like:

Host ds
	HostName server
	Port 22
	User einverne

Host github
	HostName github.com
	Port 22
	User einverne
	IdentityFile ~/.ssh/private_key_name

After all this, you can type following command to have a try:

scp filename ds:~/filename   # copy file to server
ssh ds "ls ~" 		# list server files

setup hostname

hostnamectl set-hostname example_hostname

setup timezone

设置时区:

sudo dpkg-reconfigure tzdata

Test VPS

单独总结了一篇文章来讲如何测评一个 VPS 性能

Network test

You can use this solution to solve the problem. Or there are some download test file.

Install speedtest package:

pip install speedtest-cli

or

easy_install speedtest-cli

Usage:

$ speedtest-cli -h
usage: speedtest-cli [-h] [--bytes] [--share] [--simple] [--list]
                     [--server SERVER] [--mini MINI] [--source SOURCE]
                     [--timeout TIMEOUT] [--secure] [--version]

Command line interface for testing internet bandwidth using speedtest.net.
--------------------------------------------------------------------------
https://github.com/sivel/speedtest-cli

optional arguments:
  -h, --help         show this help message and exit
  --bytes            Display values in bytes instead of bits. Does not affect
                     the image generated by --share
  --share            Generate and provide a URL to the speedtest.net share
                     results image
  --simple           Suppress verbose output, only show basic information
  --list             Display a list of speedtest.net servers sorted by
                     distance
  --server SERVER    Specify a server ID to test against
  --mini MINI        URL of the Speedtest Mini server
  --source SOURCE    Source IP address to bind to
  --timeout TIMEOUT  HTTP timeout in seconds. Default 10
  --secure           Use HTTPS instead of HTTP when communicating with
                     speedtest.net operated servers
  --version          Show the version number and exit

一些机房 100M 测速下载文件地址,用于测速之用

description: VPS 的网络性能,主要分出口和入口二个指标,入口可以用 wget 文件得到。 看下载速度,如果是 11M/s,大概就是百兆口,70M/S,大概就是 G 口。 您的 VPS 搭建好网站环境后,可以用其它的 VPS 去拽这个文件,得到出口的带宽。

Directspace 机房 /10M.100M 测试包 Portland

wget http://bandwidth.directspace.net/10MBtest.zip
wget http://bandwidth.directspace.net/100MBtest.zip

Change default shell

sudo apt install zsh
chsh -s $(which zsh)

logout and login again.

BBR

BBR 是 Google 提出的 TCP拥塞控制算法,可以使Linux服务器显著地提高吞吐量和减少TCP连接的延迟,已经提交并合并到了 Linux 内核。

检查是否开启了 BBR:

sudo sysctl net.ipv4.tcp_available_congestion_control | grep bbr
sudo sysctl net.ipv4.tcp_congestion_control | grep bbr

如果开启通常会在结果中包含 bbr。如果没有开启则使用 Teddysun 的一键脚本,注意开启之后需要重启才能生效。

wget --no-check-certificate https://github.com/teddysun/across/raw/master/bbr.sh && chmod +x bbr.sh && ./bbr.sh

docker

Docker become much powerful these days, I can build and sever all my self-host services by using Docker.

sudo apt update
sudo apt install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"
sudo apt update
apt-cache policy docker-ce
sudo apt install docker-ce
sudo systemctl status docker

或者通过一键脚本:

curl -fsSL https://get.docker.com/ | sh
# 启动并设置开机自启docker
systemctl enable --now docker

Executing the docker command without sudo 非 root 用户执行 docker 命令:

# sudo groupadd docker
sudo usermod -aG docker $USER
newgrp docker

设置后登出,然后登录。

安装 docker-compose

sudo curl -L "https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
# 如果/usr/local/bin不在PATH里
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

如果使用我的 dotfiles 配置,在 ~/.zshrc 第一次配置的时候就会把 docker-compose 二进制文件拉下来。

shadowsocks

sock5 proxy.

  • first install pip

      yum update && yum install python-setuptools
      easy_install pip
    

    or use command yum -y install python-pip to install pip

  • install shadowsocks using pip

      pip install shadowsocks
    

    just run this command

  • create json config file

      vim /etc/shadowsocks.json
    

    edit file as follow:

      {
          "server":"[ip]",
          "server_port":[port],
          "local_port":[port],
          "password":"[password]",
          "timeout":600,
          "method":"AES-256-CFB"
      }
    

    Explanation of each field:

      - server: your hostname or server IP (IPv4/IPv6).
      - server_port: server port number.
      - local_port: local port number.
      - password: a password used to encrypt transfer.
      - timeout: connections timeout in seconds.
      - method: encryption method, "bf-cfb", "aes-256-cfb", "des-cfb", "rc4", etc. Default is table, which is not secure. "aes-256-cfb" is recommended.
    
  • start server

    ssserver -c [json_path] -d start

    start service

lnmp

Second thing is to install lnmp, if you want to host a website on your VPS. You can use screen to install lnmp.

Screen can prevent network connection error during the lnmp installation. You can find more details on the lnmp official site

  1. install screen
  2. run this command: screen -S lamp to create a screen session
  3. download packages wget -c http://soft.vpser.net/lnmp/lnmp1.1-full.tar.gz
  4. uncompress the package tar zxf lnmp1.1-full.tar.gz
  5. enter directory: cd lnmp1.1-full/
  6. install lnmp If you are using Centos run ./centos.sh , If you are using Debian run ./debian.sh , If you are using Ubuntu run `./ubuntu.sh

If you’re ssh connection suddenly failed, you can connect to your server. Then run command screen -r lnmpto restore your lnmp installation.

From:http://www.vpser.net/manage/run-screen-lnmp.html

After installation, you will see some short instructions.

lnmp status manage: /root/lnmp {start|stop|reload|restart|kill|status}
default mysql root password:12345678
phpinfo : http://yourIP/phpinfo.php
phpMyAdmin : http://yourIP/phpmyadmin/
Prober : http://yourIP/p.php
Add VirtualHost : /root/vhost.sh

The path of some dirs:
mysql dir: /usr/local/mysql
php dir: /usr/local/php
nginx dir: /usr/local/nginx
web dir : /home/wwwroot/default

LNMP is a tool to auto-compile & install Nginx+MySQL+PHP on Linux
This script is a tool to Manage status of lnmp
For more information please visit http://www.lnmp.org

Usage: /root/lnmp {start|stop|reload|restart|kill|status}

reference


2015-12-08 linux , vps , lnmp

Nexus 6 tips

说是N6的Tips,当然里面很多都是Android 6.0 隐藏的功能。只要是原生 6.0 的系统都能够开启。

双击唤醒

Nexus 6 double tap to wake, root 之后安装一个app即可。

具体参考:xda-developers

开启状态栏电池百分比

原生 Android 6.0 有个小技巧能够开启状态栏的电池百分比,下拉通知栏,长按开启设置的齿轮,会打开Android 6.0 隐藏的设置,此时进入系统设置,会多出一个“System UI Tuner”,进入打开“Show embedded battery percentage”,则能在状态栏电池上显示百分比。

开启多窗口模式

另一个Android 6.0 隐藏功能,可能是官方并未完成对这个功能的开发和测试,但是就使用来看稳定性OK。开启过程如下,一句话就是修改 /system/build.prop。

  1. 复杂来说,如果手机root过,用任何可以编辑 /system/build.prop 的app,类似下面链接中提到的 build.prop.editor,或者像我一样使用 Root Explorer,直接找到文件,打开文件找到”ro.build.type”一行,将原先的”user”值修改成为”userdebug”。保存之后重启手机。
  2. 重启手机之后,进入设置,开发者选项,然后在 “drawing”设置下,开启多窗口模式。
  3. 测试多窗口。点开多任务按钮应该能看到一个黑色的框。

具体参考:xda-developers

LED for Notifications

需要第三方kernel,可以根据以下教程自行刷入。

参考:xda-developers

几大常见的kernel:

  1. Elementalx link
  2. franco.Kernel link

unlock root recovery all in one

写给自己备忘:电源键+音量下可以进入recovery mode

详情参考:xda-developers

总结

最后总结一下N6的几个缺点:

  1. 屏幕的问题,虽然是2K屏,但是 AMOLED 的屏幕黄屏问题始终存在
  2. 待机时间,也就是耗电水平,正常使用一天是肯定没有问题的,当然重度用户是怎么也不会够的。不过N6的待机时间在同等水平的机子中也并不是很好的。
  3. 不要使用涡轮快充来给手机充电,一些N6老用户使用涡轮充电给电池造成了伤害,导致手机电池膨胀,从而使得 N6 后壳开裂。

备忘

摩托罗拉官方网站显示Nexus 6将会有两个版本,分别是美国版的XT1103和国际版的XT1100,两者在基本硬件规格上大部分相同,只是支持的数据流模式和频谱略微有差别。美国版XT1103支持制式频段较少,国际版相对比较通用。

Americas Model (XT1103)
GSM/GPRS/EDGE (850, 900, 1800, 1900 MHz)
CDMA (800, 1900, secondary 800 MHz)
WCDMA (1, 2, 4, 5, 8)
LTE (2, 3, 4, 5, 7, 12, 13, 17, 25, 26, 29, 41)
CA DL (B2-B13, B2-B17, B2-29, B4-B5, B4-B13, B4-B29)

Global Model (XT1100)
GSM/GPRS/EDGE (850, 900, 1800, 1900 MHz)
WCDMA (1, 2, 4, 5, 6, 8, 9, 19)
LTE (1, 3, 5, 7, 8, 9, 19, 20, 28, 41)
CA DL (B3-B5, B3-B8)

XT1100国际版联通3G WCDMA制式可以直接使用,4G LTE部分支持1、3、5、7、8、9、19、20、28、41这些频段,国内联通和电信主要采用FDD-LTE制式中的1、3、7频段,移动则是TD-LTE中的41频段,因此理论上两种制式都可以直接使用。当然理论上永远是理论,毕竟国内运营商具体每个地方用何种频段并不一定,购买前还需要考虑这个问题。

再详细说下:

移动用户:从频段上看无论国际版还是北美版都支持band41,可以接收移动2.6G的4G信号。但用起来肯定不爽,具体原因跟Nexus 5相同,因为移动目前用于广覆盖和室内分布的TDD频段是39和40,而Nexus 6不支持这两个频段。另外Nexus 6也不支持移动3G,导致其极有可能出现Nexus 5一样的情况,接打电话后需要手工调才能返回4G待机。

联通用户:应该无忧,无论国际版还是北美版都支持band3,也就是联通的4G主频段。另外和Nexus 5一样,Nexus 6肯定也是完美支持CSFB语音回落,所以4G会用的很爽。不过话说回来,Nexus 6有的这些东西,Nexus 5国际版同样也有。

电信用户:总的来说,Nexus 6比Nexus 5强,至少在网络支持方面。如果可以接受Nexus 6的6寸巨屏。电信主力4G频段band3,两个版本的Nexus 6都支持,但仅有北美版支持CDMA,因此电信用户只能选择北美版。不过有一点要注意,电信在很多城市还用band1部署了4G,而北美版沿袭了摩托罗拉一贯的鸟样,对于美国本土不用的频段一律不支持,这次竟然连Nexus 5都支持的band1都删了。这样的后果就是,Nexus 6在未来用电信4G时,由于接收不到band1,可能网速会不如其他国内机子。不过亮点是,这次Nexus 6北美版支持Verizon的网络,这意味着,Nexus 5在电信网络上无法实现的CDMA语音回落,在Nexus 6上有99.999999%的可能会完美解决。因为Verizon与电信相同,其4G语音回落方案是:iPhone采用srlte,其他机型svlte。Nexus 5之所以没办法实现电信4G回落,是因为电信没有在基站侧部署1x CSFB,而1x CSFB是美帝Sprint支持的FDD-CDMA语音回落方案。

摘录自:机友会


2015-12-01 Nexus , Android

没有让我失望的火星救援

期待了半年的《火星救援》并没有让我感到失望。

写在观影之前

早在今年6月份看到一段预告片之后就将这部电影加入了待看片单,等了半年,在好莱坞大片云集的11月,也没有去看任何影片只等着这一部。马特达蒙,最喜欢的演员,星际宇宙,最喜欢的题材,有这这样的组合怎能不让人期待。

火星救援 马特达蒙

写在观影之后

终于在等了半年之后,在25号看了0点场,电影没有让我失望,当然带来的感动和震撼也并没有超出想象。或许是《地心引力》和《星际穿越》的铺垫,让我对此类讲述宇宙的电影有了最基本的感受,宇宙浩瀚的视觉体验,飞船对接的惊险,以及人类在广袤宇宙的渺小。

这部电影的剧情其实很简单,如果稍微做过一些了解,看电影之前应该就能预料到电影中发生的事情,而正是对于这样一部电影,我更加期待的是导演和演员如何在故事剧情整体都被观众了解的情况下去推动剧情的发展。这部电影由Andy Weir小说改编,虽然没有看过小说,但是经过小说的验证,剧情并不会离谱到哪里,所以在看完这部电影之后对整个剧情的设定,情节的发展都没有找到比较大的漏洞。电影基本在沿用两条线叙事,火星上马特达蒙的生存挑战,地球上的营救计划的展开,总体沿用这两条故事线,而中间也穿插了赫尔梅斯号飞船上的故事,到影片的结束,赫尔梅斯号上的故事线和马特达蒙的线合二为一。从剧情上来看,故事整体发展都很平淡,也像之前看到的一些评论说的那样,导演尽量的在克制并没有打温情牌,即使是在片尾才出现的爱情线,也只是淡淡的一笔带过,而主角的亲情戏也是一再的克制,犹记得其中的一个镜头就是马特坐在火星的高地上,镜头从背后绕到前面,而中间伴随着马特的遗言式的自白,这也是我唯一能够想到的提交亲情的戏份。而相比《星际穿越》中的亲情带来的感动,导演 [[Ridley Scott]] 一再的将电影的重心放到营救的整个过程中。

影片是和同学一同前去观看的,部分同学说故事发展太平淡,高潮不明显,其实这部片子的问题也是存在的,片子在极尽所能渲染出火星壮美的地貌的时候无意中拖慢的剧情。在开场火星风暴袭来快速的剧情推动之后,令人印象深刻的马特自救,种土豆之后故事发展就趋于平缓,用淡淡的叙事来铺开,当然最后我能感受到的高潮就是赫尔梅斯号去接马特的时候,这也是全片的最高潮,只是现在回想来,似乎感觉到来的太快,而结束的也太快。

再说到表演,这就不得不提到马特达蒙,几乎是用他一个人的演技支撑起了整部影片,早在《谍影重重》《天才雷普利》《心灵捕手》的时候就深深的喜欢上了这个个性演员,更让我震惊的是他曾经和本阿弗莱克凭借《心灵捕手》获取了奥斯卡最佳电影原创剧本奖,这样一位实力派的演员竟然能够在剧本创作上获得如此殊荣更让我对他刮目相看。而在这部影片中的表现同样没有让人失望,我甚至觉得马特可以凭借这部电影去拼一下奥斯卡最佳男主,令人印象深刻地几场戏出现在开头的自救,在风暴过后,马特被天线击中,他有条不紊的从身体中取出天线的情节实在让人无法忘怀。另外在更多的自白中自然地表现出那种自娱自乐的精神,也只有马特能够表现出来。像很多人说的那样,这就是一部靠马特自救和段子组成的电影。而说到配角,我竟然认出了《纸牌屋》中的那个死掉的记者,是纸牌屋中死掉了所以来拍电影了吗。然后那个黑人主管也看的眼熟却也无法想起在哪里见到过。而赫尔梅斯号上得中尉是杰西卡·查斯坦,这位演员在当时《星际穿越》的时候认识了,但是因为这部戏中的戏份也并不是很多,只在开头很果断的决策和结尾营救马特的时候有些表现,而其他方面只能通过马特说的音乐品味太差来侧面描写一下,所以也看不出演技的好坏。不过让我印象深刻的倒是那个呆萌呆萌的天体物理学家,虽然出场时间也并不是很多,但是却让人眼前一亮,并为之会心一笑。从第一个躺在床上的镜头,到后来开“爱隆会议”,他的表现非常的高效到位。他把那种科学家的自信气质和学术范儿用一种诙谐的方式表现出来,而他的方案也是整个拯救计划中很重要的部分。

在说到娱乐性,对我来说,娱乐性对于这部电影来说应该就算是话题效应了,如果从今年6月份算起,我已经期待这部电影超过了5个月了。对我来说,娱乐性的体现已经足够充分,而在11月份,好莱坞电影集中上映的前提下,我相信《火星救援》也还是依然能够保证充足的话题性。而从另一方面,电影创造出的火星场景来看,也足够具有话题性,从《地心引力》创造的宇宙世界观,《星际穿越》创造的黑洞,好莱坞几乎在以一年一部的速度刷新着我们的宇宙观。而今年几乎是 NASA 的新世纪元年,就在《火星救援》美上映的前几天,NASA 宣布火星发现水,这简直就是给这部影片一个巨大的广告宣传,再到 NASA 今年公布的冥王星的图片,整个世界都被宇宙震撼到了。

冥王星 NASA

最后在豆瓣上我会给剧情8分,表演8分,娱乐性9分。当然这都是带有私心的评分~(≧▽≦)/~啦啦啦。

电影删减

国内版删去马特达蒙光屁股一段戏。其实这里不得不吐槽一下,很多电影比这个过分的要更多,难道广电只是想刷一下存在感吗?什么都插手一脚却也并没有什么用。可是下面才是我想说的,在我告诉周围人这个删减的时候,周围人竟然说删得好,至于为什么删得好,我是无法理解的,当更重要的是或许是很多人以及习惯了这种删减的日子了吧。

写在看书之后

书里解答了我很多观影之后的疑问,帆布的疑惑,栖息仓中的爆炸等等,还有关于火星上大气的气压等等。

当然在电影过后看书的一大缺点就是在看到角色名字的时候不知不觉会在脑海里浮想起电影中的面孔,所以在看大部分沃特尼的自述的时候会想起马特达蒙。不过对我还好,我能记住的演员也就是赫尔梅斯上得几个航天员。当然下面我还要说,其实书中对这些角色的描写我觉得更成功,甚至从几句对白中表现出来的感觉抵得上电影中的画面和情节。

电影对书中内容的改编:

  1. 两辆漫游车 书中对于漫游车的改造远远超出电影中的描述,沃特尼花费了大量的时间去改造漫游车,而这部分情节在电影上被省去了,或许是从电影表现来看并不是最佳的情节。在电影后半段时期,火星上的交代变少,更多的是展现火星神奇的地貌,而书中反而不是这样,在漫游车改造的过程中,沃特尼失去了地球的联络,只能依靠自己的知识改造漫游车。并且在之后的移动过程中是没有和 NASA 的联络的。

  2. 电影中删减了很多沃特尼去往3000+公里外的 MAV 的情节 这也是在上面说到的一点,沃特尼在前往 MAV 的过程中没有人去协助,只依靠自己的努力,小说中甚至描述了他遭遇火星沙尘暴的情节,而在电影中几乎完全没有被提及,这也导致我在观影中感受到的电影中后段故事趋于平缓,当然这可能也是为了缩减电影时长而不得已为之,但是这一段沃特尼自救的部分真是惊心动魄,既紧张又充满刺激,当然在我为沃特尼这种临危不乱的精神敬佩的时候,沃特尼用自己的聪明才智已经快到达目的地了。

  3. 小说中对配角形象的塑造更加成功 这里我不得不说,小说对这些配角的形象塑造更加生动。小说中在赫尔梅斯上得情节描述较电影多,而小说对地面 NASA 等的描述较小说少,所以从小说中能看到诙谐幽默,时常开玩笑的马丁尼兹,能看到指挥果断地刘易斯指挥官,还有搞办公室恋爱的约翰森,他们的形象,加上之前在电影中留下的记忆,共同组成他们成为一个角色的特征。

  4. 对指挥官刘易斯的改编 看过电影的人应该都知道,最后是指挥官刘易斯去营救的沃特尼,其实当时看完电影并没有感觉到什么不适,但是后来想一想,虽然航天员什么方面都会训练一下,但是指挥官并没有什么特殊的理由让自己代替另一个专业航天员去营救沃特尼,当然这也可能是电影剧本的要求吧,因为我对那个人也真没多少印象,赫尔梅斯上一共六个人,上面提到的3个加上沃特尼,剩下的两个实在是没什么印象了,所以在最后电影中换成指挥官也情有可原吧。


2015-11-26 影评 , MattDamon

Git hook

和其他 Version Control System 一样,git 也有方法来触发自定义脚本。

两类 hooks:

  • client hooks
  • server hooks

Installing a hook

hook 脚本在 hooks 子目录下,大部分是 .git/hooks 下。在使用 git init 之后就会初始化一些 sample 脚本,在 hooks 下都以 .sample 结尾,如果要使用则需要将 .sample 后缀去掉。

Client-side Hooks

pre-commit

pre-commit hook 会在输入 commit message 之前被执行。

通常可以在该 hook 中检查代码是否被提交,运行 test,代码格式检查,或者其他 lint 工具检查。如果脚本返回 non-zero 值会阻断 commit。

prepare-commit-msg

prepare-commit-msg hook 会在 commit message 编辑器被调用前,在默认 message 被创建后被触发。这可以使得你可以自定义默认 message。这个 hook 接受一些参数:

  • commit message 的文件路径
  • commit 类型
  • 如果是 amended 提交则包含 commit SHA-1

commit-msg

commit-msg hook 接受一个参数,可以在该 hook 中检查 commit message。

post-commit

在整个 commit 结束后, post-commit hook 会执行。不接受任何参数。通常该 hook 用来发送一些通知。

Other Hooks

Server-Side Hooks

reference


2015-11-21 git , git-hook , vcs

Java 查漏补缺之 throwable vs exception 区别

在 java 中 try catch 的时候,大多数情况下是使用的 Exception,但是今天看代码有些却 catch 了 Throwable,于是总结下。

看 JDK 源码知道 Throwable 是 Exception 的超类,也同样是 Error 的超类,所以可想而知,如果 catch 了 Throwable,那么会连同 Exception 和 Error 一同 catch,这样也不会丢异常。

  • Throwable 是所有异常的根,java.lang.Throwable
  • Error 是错误,java.lang.Error,Error 通常是不可恢复的错误
  • Exception 是异常,java.lang.Exception,Exception 通常是程序可恢复的

当程序发生不可控错误,抛出 Error 错误,与异常不同的是 Error 及其子类对象不应该被抛出。通常发生 Error 时需要介入处理。

reference


2015-11-20 java , jdk , exception

Awesome vim plugin website collections

功能比较强大,比较重要的几个 Plugin 都在单独的文章中做了介绍,这里单独的列举一些特定场景使用的插件,带有语法高亮等的插件,比如针对 Nginx 配置, Dockerfile 文件等等的插件。

Plugins

优化 nginx 配置

Plug 'chr4/nginx.vim'

Python

Go

Plug 'fatih/vim-go'

js

Plug 'kchmck/vim-coffee-script'
" CoffeeScript
Plugin 'mtscout6/vim-cjsx'

vimawesome


2015-11-03 vim , awesome , collection , collections

电子书

本站提供服务

最近文章

  • AI Shell 让 AI 在命令行下提供 Shell 命令 AI Shell 是一款在命令行下的 AI 自动补全工具,当你想要实现一个功能,敲一大段命令又记不住的时候,使用自然语言让 AI 给你生成一个可执行的命令,然后确认之后执行。
  • 最棒的 Navidrome 音乐客户端 Sonixd(Feishin) Sonixd 是一款跨平台的音乐播放器,可以使用 [[Subsonic API]],兼容 Jellyfin,[[Navidrome]],Airsonic,Airsonic-Advanced,Gonic,Astiga 等等服务端。 Sonixd 是一款跨平台的音乐播放器,可以使用 [[Subsonic API]],兼容 Jellyfin,[[Navidrome]],Airsonic,Airsonic-Advanced,Gonic,Astiga 等等服务端。
  • 中心化加密货币交易所 Gate 注册以及认证 Gate.io 是一个中心化的加密货币交易所。Gate 中文通常被称为「芝麻开门」,Gate 创立于 2013 年,前身是比特儿,是一家致力于安全、稳定的数字货币交易所,支持超过 1600 种数字货币的交易,提供超过 2700 个交易对。
  • 不重启的情况下重新加载 rTorrent 配置文件 因为我在 Screen 下使用 rTorrent,最近经常调试修改 rtorrent.rc 配置文件,所以想要找一个方法可以在不重启 rTorrent 的情况重新加载配置文件,网上调查了一下之后发现原来挺简单的。
  • Go 语言编写的网络穿透工具 chisel chisel 是一个在 HTTP 协议上的 TCP/UDP 隧道,使用 Go 语言编写,10.9 K 星星。