ffmpeg将文件推流成实时ts流,将文件推成rtsp流

ffmpeg将文件推流成实时ts流

命令

./ffmpeg -re -i /home/tuners/ffmpeg/yqxs.mp3 -acodec copy -flush_packets  1 -f mpegts  udp://$transcode_server_ip:5162?localport=49088

ffmpeg -re -i /home/tuners/ffmpeg/yqxs.mp3 -acodec copy -flush_packets  1 -f mpegts  udp://$transcode_server_ip:5162?localport=49088

ffmpeg 为进程名,当然也可以加上全路径

/home/tuners/ffmpeg/yqxs.mp3 为音频文件路径

-f mpegts 输出格式为mpegts

udp://$transcode_server_ip:5162?localport=49088  为udp地址,$transcode_server_ip是输出地址,5162是输出端口,?localport=49088是本地端口,可以根据情况进行修改

ffmpeg将文件推流成实时rtsp流

命令

ffmpeg -re -i "d:\yqxs.mp3" -acodec mp3   -f rtsp rtsp://ip:port/stream_name.sdp

ffmpeg  -re  -i "d:\yqxs.mp3" -acodec mp3  -f rtsp rtsp://ip:port/stream_name.sdp

-----------------------------------------说明------------------------------------------------------------------

ffmpeg 为进程名称,当然也可以在前面带上全路径

"d:\yqxs.mp3" 为音频文件全路径

-acodec mp3 说明推流的音频格式为mp3

 -f rtsp 说明推送成rtsp流

rtsp://ip:port/stream_name.sdp  为推成rtsp流之后的rtsp地址

运行方式

命令行运行

安装 windows按钮 ,输入cmd,以管理员身份运行

输入命令执行即可

编写bat脚本运行

在ffmpeg进程所在目录下新建bat脚本

将命令拷贝进去,保存

双击bat运行或者右键bat选择管理员运行

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要将图片推送为RTSP视频,需要使用FFmpeg和libx264编码器。以下是一个C语言示例程序,说明如何将单个图片推送为RTSP视频。 首先,需要包含必要的头文件: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdint.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <libavutil/opt.h> #include <libavformat/avformat.h> #include <libswscale/swscale.h> ``` 然后,需要定义一些常量和变量: ```c #define WIDTH 640 #define HEIGHT 480 #define FPS 30 #define BITRATE 500000 AVFormatContext *fmt_ctx = NULL; AVStream *video_stream = NULL; AVCodecContext *codec_ctx = NULL; AVCodec *codec = NULL; AVPacket pkt; AVFrame *frame = NULL; uint8_t *frame_buffer = NULL; struct SwsContext *sws_ctx = NULL; int sockfd = -1; struct sockaddr_in server_addr; ``` 接下来,需要初始化FFmpeg库和网络套接字: ```c av_register_all(); avformat_network_init(); if (avformat_alloc_output_context2(&fmt_ctx, NULL, "rtsp", "rtsp://127.0.0.1:8554/live") < 0) { fprintf(stderr, "Error allocating output context.\n"); exit(1); } codec = avcodec_find_encoder(AV_CODEC_ID_H264); if (!codec) { fprintf(stderr, "Codec not found.\n"); exit(1); } video_stream = avformat_new_stream(fmt_ctx, codec); if (!video_stream) { fprintf(stderr, "Failed to create new stream.\n"); exit(1); } codec_ctx = video_stream->codec; codec_ctx->codec_id = AV_CODEC_ID_H264; codec_ctx->codec_type = AVMEDIA_TYPE_VIDEO; codec_ctx->pix_fmt = AV_PIX_FMT_YUV420P; codec_ctx->width = WIDTH; codec_ctx->height = HEIGHT; codec_ctx->time_base = (AVRational){1, FPS}; codec_ctx->bit_rate = BITRATE; codec_ctx->gop_size = FPS; codec_ctx->max_b_frames = 0; codec_ctx->qmin = 10; codec_ctx->qmax = 51; codec_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER; if (avcodec_open2(codec_ctx, codec, NULL) < 0) { fprintf(stderr, "Failed to open codec.\n"); exit(1); } frame = av_frame_alloc(); frame->format = codec_ctx->pix_fmt; frame->width = codec_ctx->width; frame->height = codec_ctx->height; av_frame_get_buffer(frame, 0); frame_buffer = (uint8_t *)malloc(avpicture_get_size(codec_ctx->pix_fmt, codec_ctx->width, codec_ctx->height)); avpicture_fill((AVPicture *)frame, frame_buffer, codec_ctx->pix_fmt, codec_ctx->width, codec_ctx->height); sws_ctx = sws_getContext(WIDTH, HEIGHT, AV_PIX_FMT_RGB24, codec_ctx->width, codec_ctx->height, codec_ctx->pix_fmt, SWS_BICUBIC, NULL, NULL, NULL); sockfd = socket(AF_INET, SOCK_DGRAM, 0); if (sockfd < 0) { fprintf(stderr, "Failed to create socket.\n"); exit(1); } memset(&server_addr, 0, sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_port = htons(8554); server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); ``` 接下来,需要循环读取图片并将其编码为H.264: ```c while (1) { AVFrame *rgb_frame = av_frame_alloc(); uint8_t *rgb_frame_buffer = (uint8_t *)malloc(WIDTH * HEIGHT * 3); FILE *fp = fopen("image.jpg", "rb"); fread(rgb_frame_buffer, 1, WIDTH * HEIGHT * 3, fp); fclose(fp); avpicture_fill((AVPicture *)rgb_frame, rgb_frame_buffer, AV_PIX_FMT_RGB24, WIDTH, HEIGHT); sws_scale(sws_ctx, rgb_frame->data, rgb_frame->linesize, 0, HEIGHT, frame->data, frame->linesize); av_frame_free(&rgb_frame); free(rgb_frame_buffer); int ret = avcodec_send_frame(codec_ctx, frame); if (ret < 0) { fprintf(stderr, "Error sending frame to encoder.\n"); exit(1); } while (ret >= 0) { ret = avcodec_receive_packet(codec_ctx, &pkt); if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { break; } else if (ret < 0) { fprintf(stderr, "Error receiving packet from encoder.\n"); exit(1); } pkt.stream_index = video_stream->index; av_packet_rescale_ts(&pkt, codec_ctx->time_base, video_stream->time_base); av_interleaved_write_frame(fmt_ctx, &pkt); sendto(sockfd, pkt.data, pkt.size, 0, (struct sockaddr *)&server_addr, sizeof(server_addr)); av_packet_unref(&pkt); } usleep(1000000 / FPS); } ``` 最后,需要释放资源并关闭套接字: ```c av_write_trailer(fmt_ctx); avcodec_free_context(&codec_ctx); av_frame_free(&frame); sws_freeContext(sws_ctx); free(frame_buffer); close(sockfd); avformat_free_context(fmt_ctx); avformat_network_deinit(); ``` 完整的示例程序可以在以下链接中找到:https://github.com/FFmpeg/FFmpeg/blob/master/doc/examples/encode_video.c
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

三希

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值