基于Live555的封装H264裸流为Ts流笔记(H264转ts流最简单方法)


最近做Live555相关项目,以前用ffmpeg做Ts流的转换,有时由于264编码的部分细节,ffmpeg从h264直接转ts流存在兼容问题,而使用live555方面则简单很多。

贴个代码,看官你可以直接抄:


/**********
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the
Free Software Foundation; either version 2.1 of the License, or (at your
option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)

This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
more details.

You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
**********/
// Copyright (c) 1996-2013, Live Networks, Inc.  All rights reserved
// A program that converts a H.264 (Elementary Stream) video file into a Transport Stream file.
// main program

#include "liveMedia.hh"
#include "BasicUsageEnvironment.hh"

char const* inputFileName = "in.264";
char const* outputFileName = "out.ts";

void afterPlaying(void* clientData); // forward

UsageEnvironment* env;

int main(int argc, char** argv) {
  // Begin by setting up our usage environment:
  TaskScheduler* scheduler = BasicTaskScheduler::createNew();
  env = BasicUsageEnvironment::createNew(*scheduler);

  // Open the input file as a 'byte-stream file source':
  FramedSource* inputSource = ByteStreamFileSource::createNew(*env, inputFileName);
  if (inputSource == NULL) {
    *env << "Unable to open file \"" << inputFileName
	 << "\" as a byte-stream file source\n";
    exit(1);
  }

  // Create a 'framer' filter for this file source, to generate presentation times for each NAL unit:
  H264VideoStreamFramer* framer = H264VideoStreamFramer::createNew(*env, inputSource, True/*includeStartCodeInOutput*/);

  // Then create a filter that packs the H.264 video data into a Transport Stream:
  MPEG2TransportStreamFromESSource* tsFrames = MPEG2TransportStreamFromESSource::createNew(*env);
  tsFrames->addNewVideoSource(framer, 5/*mpegVersion: H.264*/);
  
  // Open the output file as a 'file sink':
  MediaSink* outputSink = FileSink::createNew(*env, outputFileName);
  if (outputSink == NULL) {
    *env << "Unable to open file \"" << outputFileName << "\" as a file sink\n";
    exit(1);
  }

  // Finally, start playing:
  *env << "Beginning to read...\n";
  outputSink->startPlaying(*tsFrames, afterPlaying, NULL);

  env->taskScheduler().doEventLoop(); // does not return

  return 0; // only to prevent compiler warning
}

void afterPlaying(void* /*clientData*/) {
  *env << "Done reading.\n";
  *env << "Wrote output file: \"" << outputFileName << "\"\n";
  exit(0);
}



参考文章:

es,pes,ts互相转换

http://download.csdn.net/detail/ykl0602/4423361#comment

ES PES TS

http://blog.csdn.net/shuyong1999/article/details/7094151

TS流的解码过程-ES-PES-DTS-PTS-PCR

http://blog.csdn.net/godspirits/article/details/5653381






  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在Linux上使用FFmpeg推流RTMPh264/PC的步骤如下: 1. 首先,确保你已经安装了FFmpeg。如果没有安装,可以使用以下命令进行安装: ``` sudo apt-get install ffmpeg ``` 2. 使用以下命令将h264视频和PCMU音频推送到RTMP服务器: ``` ffmpeg -re -i input.mp4 -c:v libx264 -preset veryfast -tune zerolatency -b:v 2000k -maxrate 2000k -bufsize 2000k -pix_fmt yuv420p -g 50 -c:a pcm_mulaw -ar 8000 -f flv rtmp://server/live/stream ``` 解释一下上述命令的参数: - `-re`:以实时速度读取输入文件。 - `-i input.mp4`:指定输入文件的路径和名称。 - `-c:v libx264`:使用libx264编码器进行视频编码。 - `-preset veryfast`:设置视频编码速度为veryfast。 - `-tune zerolatency`:设置视频编码器为零延迟模式。 - `-b:v 2000k`:设置视频的比特率为2000k。 - `-maxrate 2000k`:设置视频的最大比特率为2000k。 - `-bufsize 2000k`:设置视频的缓冲区大小为2000k。 - `-pix_fmt yuv420p`:设置像素格式为yuv420p。 - `-g 50`:设置关键帧间隔为50帧。 - `-c:a pcm_mulaw`:使用PCM mu-law编码器进行音频编码。 - `-ar 8000`:设置音频的采样率为8000Hz。 - `-f flv`:指定输出格式为FLV。 - `rtmp://server/live/stream`:指定RTMP服务器的URL和名称。 请根据你的实际情况修改输入文件的路径和名称,以及RTMP服务器的URL和名称。 希望以上信息对你有帮助!如果你还有其他问题,请继续提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值