昨天要在视频转发程序里增加转码功能:就是将输入(流/文件)转码成指定格式的视频流和音频流。
研究了一下,发现vlc里自带了转码功能,只不过参数设置比较麻烦。
我们具体的需求是:
输入:rtsp流或者udp组播数据(对设置转码参数来说,其实没有区别)
输出:H264视频(644k)+mp2音频(56k),总码率700k,ts封装,使用udp转发到本地单播
调用libvlc时的完整转码参数为:
sendudp = "--sout=#duplicate{dst=\"transcode{venc=x264{profile=baseline},vcodec=h264,acodec=mpga,vb=644,ab=56}:std{access=udp,mux=ts,dst=127.0.0.1:" + m_strport + "}\"}"
如果是直接使用vlc播放器来转码,命令行为:
vlc udp://@230.0.0.1:8001 --sout '#duplicate{dst="transcode{venc=x264{profile=baseline},vcodec=h264,acodec=mpga,vb=644,ab=56}:std{access=udp,mux=ts,dst=127.0.0.1:18009}"}'
考虑到以后转码参数可能会发生变化,就把它做到配置文件中了,用户在使用时可以按需求配置相应的转码参数,不配值的话则不进行转码。
这样,我们在启动libvlc进行转发(转码)时的代码如下:
1 void CInputVideoVLC::InitVlc(int localport)
2 {
3 string m_strport = "";
4 string sendudp = "";
5 m_strport = CStringExt::Int2String(localport);
6 //sendudp = "--sout=#duplicate{dst=std{access=udp,mux=ts,dst=127.0.0.1:" + m_strport + "}}";
7
8 //transcode
9 sendudp = "--sout=#duplicate{dst=\"";
10 //sendudp += "transcode{venc=x264{profile=baseline},vcodec=h264,acodec=mpga,vb=644,ab=56}:";
11 sendudp += m_strTranscode;
12 sendudp += "std{access=udp,mux=ts,dst=127.0.0.1:" + m_strport + "}\"}";
13
14 const char * const vlc_args[] = {
15 "-I", "dummy", // Don't use any interface
16 "--ignore-config", // Don't use VLC's config
17 "--extraintf=logger", //log anything
18 "--verbose=0", //be much more verbose then normal for debugging purpose
19 "--repeat",
20 sendudp.c_str()
21 };
22 //printf("sendudp:%s\n", sendudp.c_str());
23
24 libvlc_exception_init(&_vlcexcep); //Initialize an exception structure
25 _vlcinstance = libvlc_new(sizeof (vlc_args) / sizeof (vlc_args[0]), vlc_args, &_vlcexcep); //Calculation character space
26 raise(&_vlcexcep);
27 _mp = libvlc_media_player_new(_vlcinstance, &_vlcexcep); //Create an empty Media Player object
28 raise(&_vlcexcep);
29 }
具体的转码参数说明,可以查看vlc文档:http://www.videolan.org/doc/streaming-howto/en/ch03.html
以及相关示例:http://www.videolan.org/doc/streaming-howto/en/ch04.html
还可以参考:http://www.cnitblog.com/buf/archive/2011/08/06/74993.html