通过ffmpeg管道直播,ffmpeg启动完成后消失了。

Mr Dang 2020-11-18 10:45:46
ffmpeg version N-99925-g96443ab4b0 Copyright (c) 2000-2020 the FFmpeg developers
built with gcc 9.3-win32 (GCC) 20200320
configuration: --prefix=/ffbuild/prefix --pkg-config-flags=--static --pkg-config=pkg-config --cross-prefix=x86_64-w64-mingw32- --arch=x86_64 --target-os=mingw32 --enable-gpl --enable-version3 --disable-debug --enable-shared --disable-static --disable-debug --disable-w32threads --enable-pthreads --enable-iconv --enable-zlib --enable-libxml2 --enable-libfreetype --enable-libfribidi --enable-gmp --enable-lzma --enable-fontconfig --enable-opencl --enable-libvmaf --enable-vulkan --enable-libvorbis --enable-amf --enable-libaom --enable-avisynth --enable-libdav1d --enable-libdavs2 --enable-ffnvcodec --enable-cuda-llvm --enable-libglslang --enable-libass --enable-libbluray --enable-libmp3lame --enable-libopus --enable-libtheora --enable-libvpx --enable-libwebp --enable-libmfx --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librav1e --enable-librubberband --enable-schannel --enable-sdl2 --enable-libsoxr --enable-libsrt --enable-libsvtav1 --enable-libtwolame --enable-libuavs3d --enable-libvidstab --enable-libx264 --enable-libx265 --enable-libxavs2 --enable-libxvid --enable-libzimg --extra-cflags=-DLIBTWOLAME_STATIC --extra-cxxflags= --extra-ldflags=-pthread --extra-libs=-lgomp
libavutil 56. 60.100 / 56. 60.100
libavcodec 58.112.103 / 58.112.103
libavformat 58. 64.100 / 58. 64.100
libavdevice 58. 11.102 / 58. 11.102
libavfilter 7. 90.100 / 7. 90.100
libswscale 5. 8.100 / 5. 8.100
libswresample 3. 8.100 / 3. 8.100
libpostproc 55. 8.100 / 55. 8.100
+1
Input #0, rawvideo, from '\\.\pipe\pusher3':
线程 '<无名称>' (0x18fc) 已退出,返回值为 0 (0x0)。
Duration: N/A, start: 0.000000, bitrate: 110592 kb/s
Stream #0:0: Video: rawvideo (BGR[24] / 0x18524742), bgr24, 1280x720, 110592 kb/s, 5 tbr, 5 tbn, 5 tbc
Stream mapping:
Stream #0:0 -> #0:0 (rawvideo (native) -> h264 (libx264))
Press [q] to stop, [?] for help
[libx264 @ 0000011bf583b1c0] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
[libx264 @ 0000011bf583b1c0] profile Constrained Baseline, level 3.1, 4:2:0, 8-bit
[libx264 @ 0000011bf583b1c0] 264 - core 161 - H.264/MPEG-4 AVC codec - Copyleft 2003-2020 - http://www.videolan.org/x264.html - options: cabac=0 ref=1 deblock=0:0:0 analyse=0:0 me=dia subme=0 psy=1 psy_rd=1.00:0.00 mixed_ref=0 me_range=16 chroma_me=1 trellis=0 8x8dct=0 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=0 threads=6 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=0 weightp=0 keyint=250 keyint_min=5 scenecut=0 intra_refresh=0 rc=crf mbtree=0 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=0
Output #0, flv, to 'rtmp://192.168.1.202/live/3':
Metadata:
encoder : Lavf58.64.100
Stream #0:0: Video: h264 (libx264) ([7][0][0][0] / 0x0007), yuv420p(tv, progressive), 1280x720, q=-1--1, 5 fps, 1k tbn, 5 tbc
Metadata:
encoder : Lavc58.112.103 libx264
Side data:
cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: N/A
[flv @ 0000011bf5839700] Failed to update header with correct duration.
[flv @ 0000011bf5839700] Failed to update header with correct filesize.
frame= 1 fps=0.0 q=20.0 Lsize= 406kB time=00:00:00.00 bitrate=3322624.0kbits/s speed=0.0174x
video:405kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.074937%
[libx264 @ 0000011bf583b1c0] frame I:1 Avg QP:20.00 size:414433
[libx264 @ 0000011bf583b1c0] mb I I16..4: 100.0% 0.0% 0.0%
[libx264 @ 0000011bf583b1c0] coded y,uvDC,uvAC intra: 97.5% 89.8% 85.8%
[libx264 @ 0000011bf583b1c0] i16 v,h,dc,p: 18% 13% 55% 14%
[libx264 @ 0000011bf583b1c0] i8c dc,h,v,p: 54% 20% 18% 8%
[libx264 @ 0000011bf583b1c0] kb/s:16577.32
打印到这里,ffmpeg.exe进程就自己消失了?有人能帮忙看下怎么回事?



using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace IPClient.Core
{
public class RtmpPusher
{
private int _camId;
private readonly string _ffmpeg_path;
private readonly string _rtmp_url;
private ConcurrentQueue<byte[]> _bgr24Buffer = new ConcurrentQueue<byte[]>();

public RtmpPusher(int camId)
{
this._camId = camId;
this._ffmpeg_path = @"D:\quickhigh\ffmpeg\bin\ffmpeg.exe";
this._rtmp_url = string.Format("rtmp://192.168.1.202/live/{0}", camId);
string pipeName = string.Format("pusher{0}", camId);
Task.Factory.StartNew((p) =>
{
DoWriteWork(p as string);
}, pipeName, TaskCreationOptions.LongRunning);
Task.Factory.StartNew((p) =>
{
DoRunFFMPEG(p as string);
}, pipeName, TaskCreationOptions.LongRunning);
}

public RtmpPusher StartNew(int camId)
{
return new RtmpPusher(camId);
}

public void AddBuffer(byte[] buffer)
{
if (_bgr24Buffer.IsEmpty)
{
int height = 720;
int width = 1280;
int dataLen = width * height * 3;
byte[] rgb24_buffer = new byte[width * height * 3];
//54字节是BMP固定信息
Array.Copy(buffer, 54, rgb24_buffer, 0, dataLen);
//bmp位图数据存储时是B、G、R排列,变成RGB24时G不动调换R、B。
for (int j=0;j< height; j++)
{
for (int i = 0; i < width; i++)
{
byte temp = rgb24_buffer[(j * width + i) * 3 + 2];
rgb24_buffer[(j * width + i) * 3 + 2] = rgb24_buffer[(j * width + i) * 3 + 0];
rgb24_buffer[(j * width + i) * 3 + 0] = temp;
}
}
_bgr24Buffer.Enqueue(rgb24_buffer);
}
}

private void DoWriteWork(string pipeName)
{
byte[] buffer = null;
using (NamedPipeServerStream pipeServer =new NamedPipeServerStream(pipeName, PipeDirection.InOut, 1))
{
pipeServer.WaitForConnection();
do
{
try
{
if (_bgr24Buffer.TryDequeue(out buffer))
{
using (StreamWriter sw = new StreamWriter(pipeServer))
{
Trace.WriteLine("+1");
char[] cBuffer = Encoding.ASCII.GetChars(buffer);
sw.Write(cBuffer, 0, cBuffer.Length);
sw.Flush();
}
}
}
catch
{ }
finally
{
Thread.Sleep(1);
}

}
while (pipeServer.IsConnected);
}
}

private void DoRunFFMPEG(string pipeName)
{
string args = string.Format(@"-y -f rawvideo -vcodec rawvideo -pix_fmt bgr24 -s 1280x720 -r 5 -i \\.\pipe\{0} -c:v libx264 -pix_fmt yuv420p -preset ultrafast -f flv ""{1}""", pipeName, _rtmp_url);
RunFFMPEG(args);
}

private void RunFFMPEG(string args)
{
Process p = new Process();
p.StartInfo.FileName = _ffmpeg_path;
p.StartInfo.Arguments = args;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = false;
p.ErrorDataReceived += new DataReceivedEventHandler(ffmpeg_Output);
p.Start();
p.BeginErrorReadLine();
p.WaitForExit();//阻塞等待进程结束
p.Close();
p.Dispose();
}

private void ffmpeg_Output(object sender, DataReceivedEventArgs e)
{
Trace.WriteLine(e.Data);
}
}
}

...全文
3315 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
Mr Dang 2020-11-19
  • 打赏
  • 举报
回复
自己解决了,附上链接 https://blog.csdn.net/a295281315/article/details/109806365

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧