mplayer命令使用方法:
mplayer [options] filename
**注:设置系统环境变量后,才可以在任意路径启动mplayer **
下面直接说Qt程序下mplayer在slave从模式的命令发送:
-slave命令: 表示mplayer 启动从模式 ,不受终端控制台控制,也不在截获键盘事件,只能通过程序给mplayer发送控制命令,而且每个命令必须以\n区分,不然mplayer不能识别。这里说的是每个命令,而不是一串命令,下面会有详细讲解。
-quiet命令: 不输出冗余的信息
-af命令: 调节速度时,使得声音不变 ,否则声音会变得很混或者很尖锐,scaletempo为mplayer插件
-speed命令: 设置播放速度 ,1为正常播放
-wid命令: 将mplayer投放到指定的ID的窗口(每个窗口都有其winId,Qt程序的控件有winId()这个函数)
例子:
QProcess *movieProcess = new QProcess;
QStringList argments;
argments << "-slave"<<"-quiet" << "-af"<<"scaletempo"<<"-speed"<<"1"
<< "-wid" <<QString::number(winId)<< fileName;
movieProcess->start("D:/Program Files/mplayer/mplayer.exe",argments);
-slave模式下,每个命令以\n结尾,mplayer才能辨别发过来的命令,而QStringList变量,每<<一个字段,系统会自动补一个换行符"\n",所以符合发送命令的要求。
例子说明:argments <<"-slave" << “-quiet"不能写为argments <<”-slave -quiet",因为"-slave -quiet"中间没有"\n"换行符,也不能"-slave\n -quiet"这么写,因为这样系统会认为这个“\n”是普通字符,而不是换行符。
那mplayer在slave模式下,Qt要怎么通过进程给正在运行的mplayer发送命令呢?
通过QProcess类的write()函数发送命令
//设置播放速度,声音不变
QString cmd;
cmd= QString("af scaletempo\n"); //不需要再写-,每个命令\n结尾
movieProcess->write(cmd.toUtf8());
cmd= QString("speed_set %1\n").arg(QString().number(rateBox->currentText().left(3).toFloat()));
movieProcess->write(cmd.toUtf8());
Qt终端又怎么接收mplayer的输出信息呢?
mplayer的输出信息存放在标准输出缓存区里面,通过QProcess::readAllStandardOutput()等相关函数读取,当有数据来时,系统会自动发送QProcess::readyReadStandardOutput()信号。
//绑定信号与槽函数
connect(movieProcess,&QProcess::readyReadStandardOutput,this,&PlayerControl::readPlayerStdOutInfo);
//获取影片时间长度
movieProcess->write("get_time_length\n");
//获取影片的分辨率
movieProcess->write("get_video_resolution\n");
//槽函数打印标准输出数据
void PlayerControl::readPlayerStdOutInfo()
{
QString outputInnfo = movieProcess->readAllStandardOutput();
qDebug()<< outputInnfo;
}
//得到分辨率
get_video_resolution
Print out the video resolution of the current file.
//影片长度(总秒数)
get_time_length
Print out the length of the current file in seconds.
//影片当前时间点
get_time_pos
Print out the current position in the file in seconds, as float.
get_percent_pos
Print out the current position in the file, as integer percentage [0-100).
//影片文件名
get_file_name
Print out the name of the current file.
//降帧率
frame_drop [value]
Toggle/set frame dropping mode.
//切换影片
alt_src_step (ASX playlist only)
When more than one source is available it selects the next/previous one.
//设置无声
mute [value]
Toggle sound output muting or set it to [value] when [value] >= 0
(1 == on, 0 == off).
//暂停
pause
Pause/unpause the playback.
//退出
quit [value]
Quit MPlayer. The optional integer [value] is used as the return code
for the mplayer process (default: 0).
//影片时间位置偏移
seek [type]
Seek to some place in the movie.
0 is a relative seek of +/- seconds (default).
1 is a seek to % in the movie.
2 is a seek to an absolute position of seconds.
//增加播放速度
speed_incr
Add to the current playback speed.
//设置播放速度
speed_set
Set the speed to .
用上面的方法加速播放后,声音也会加速,频率变快,因此音调非常尖锐刺耳,这个问题可以通过ScaleTempo插件解决。这个插件默认包含在MPlayer中。只要通过下面命令:
//加速且保持正常音调
-af scaletempo -speed 1.5
//停止播放
stop
Stop playback.
//纵横比
switch_ratio [value]
Change aspect ratio at runtime. [value] is the new aspect ratio expressed
as a float (e.g. 1.77778 for 16/9).
There might be problems with some video filters.
//全屏
vo_fullscreen [value]
Toggle/set fullscreen mode.
//音量调节
volume [abs]
Increase/decrease volume or set it to if [abs] is nonzero.