将RTP序列有效载荷转换为.wav文件
Convert a RTP sequence payload in a .wav file
我有一个文本文件,其有效载荷(十六进制)约为VoIP会话的RTP数据包,有人知道如何将文本转换为文件吗。使用c/c++的wav音频?
PS:我使用的是GNU/Linux。
感谢
我对Java也做同样的事情。这是一个我用于测试目的的类,用于接收UDP RTP数据包(uLaw)并将其保存到WAV文件中。有关如何使用的示例,请参阅主要方法
public class UdpDataReceiver implements Runnable {
private boolean isRunning = true;
private int port = -1;
private DatagramSocket socket;
public UdpDataReceiver(int port) {
this.port = port;
}
public void stop() {
isRunning = false;
socket.close();
}
@Override
public void run() {
try {
socket = new DatagramSocket(port);
System.out.println(" + Listening for UDP data on port " + port + ".");
} catch (Exception e) {
e.printStackTrace();
}
// Size of data buffer is insignificant; it just needs to be big enough to hold the UDP packet (or it will be truncated).
byte[] data = new byte[512];
DatagramPacket packet = new DatagramPacket(data, data.length);
String file = null;
FileOutputStream fos = null;
try {
// For now we'll just dump to a file.
file = "C:\tmp\" + System.currentTimeMillis() + ".raw";
fos = new FileOutputStream(file);
while (isRunning) {
try {
socket.receive(packet);
} catch (SocketException se) {
// If we're just trying to tell the socket we're done, we'll get a SocketException.
if (isRunning) {
se.printStackTrace();
}
}
if (packet.getLength() > 12) {
// Strip off 12 bytes of header data.
fos.write(packet.getData(), packet.getOffset() + 12, packet.getLength() - 12);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fos.flush();
fos.close();
convertULawFileToWav(file);
} catch (Exception e) {
}
System.out.println(" + Closed port " + port + " and wrote UDP packets to file: " + file);
}
}
public static void convertULawFileToWav(String filename) {
File file = new File(filename);
if (!file.exists())
return;
try {
long fileSize = file.length();
int frameSize = 160;
long numFrames = fileSize / frameSize;
AudioFormat audioFormat = new AudioFormat(Encoding.ULAW, 8000, 8, 1, frameSize, 50, true);
AudioInputStream audioInputStream = new AudioInputStream(new FileInputStream(file), audioFormat, numFrames);
AudioSystem.write(audioInputStream, Type.WAVE, new File(file.getAbsolutePath().substring(0, file.getAbsolutePath().length() - 3) + "wav"));
} catch (IOException e) {
e.printStackTrace();
}
}
// For testing...
public static void main(String[] args) throws InterruptedException {
UdpDataReceiver r1 = new UdpDataReceiver(65337);
new Thread(r1).start();
// Send data...
Thread.sleep(30000);
r1.stop();
}
}
相关文章:
- 防止主数据类型C++的隐式转换
- 模板参数替换失败,并且未完成隐式转换
- 努力将整数转换为链表。不知道我在这里做错了什么
- HEX值到wchar_t字符(UTF-8)的转换
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将 Qvector<uint8_t> 转换为 QString
- 如何在cuSparse中使用cusparseXcoo2csr从coo转换为csc
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 在c++中使用nlohmann从类到json的转换
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 将Integer转换为4字节的unsined字符矢量(按大端字节顺序)
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- 复制列表初始化的隐式转换的等级是多少
- 将RTP序列有效载荷转换为.wav文件
- 实时录制/转换音频数据到WAV
- 将WAV文件音频输入转换为纯ASCII字符
- 在Android上从mp3到WAV的快速转换
- 如何使用C或c++将.caf文件转换为.wav文件?
- 音频转换(跛脚)C++WAV -> MP3