wang-bin / mdk-sdk

multimedia development kit. download:
https://sourceforge.net/projects/mdk-sdk/files/
291 stars 33 forks source link

保存本地摄像头的H264视频流失败 #215

Closed feiyangqingyun closed 5 months ago

feiyangqingyun commented 5 months ago

测试过264/265源头的视频文件和视频流,都能正常保存。唯独H264格式的本地摄像头这种视频流无法正常保存。不知道是不是mdk中做了特殊限制,不允许本地摄像头的视频流保存。用ffmpeg测试过了,保存正常,说明是行得通的。

feiyangqingyun commented 5 months ago

一般本地摄像头是rawvideo数据或者mjpeg的,但是这几年随着直播的兴起,出现好多直接是264的本地摄像头,这种不用编码直接可以推流和保存。

feiyangqingyun commented 5 months ago
try to start to record
url is not supported by MediaIO FFmpeg
url is not supported by MediaIO Mem
url is not supported by MediaIO WinRT
Try to use default MediaIO
0000000018577280 open url: 
0000000018577280 url opened
muxer output format name: mp4
stream map: 0=>0
[FFmpeg:?] Output #0, mp4, to 'H:/codevideo/bin/video/2024-06-13-13-48-47-254.mp4':
[FFmpeg:?]   Stream #0
[FFmpeg:?] :0
[FFmpeg:?] : Video: h264 (Main), yuvj420p(pc, bt709), 1920x1080, q=2-31
[FFmpeg:?] 
000000001B9E8100 avio set class
Format: mp4, range: 0 +0ms, bitrate: 0, size: 0
Metadata:
  encoder: Lavf61.1.100
Streams: 1
 Video:
  stream#0, range: 0 +0ms, frames: 0
  codec: h264 tag: 'avc1' profile: 77 level: 41, yuv420p, bpc:8, bpp:12(8,8,8), channels:(1,1,1), map: 0 1 2 0, bitrate: 0, 1920x1080, fps: 0, bframes: 0, primaries: BT709, trc: BT709, matrix: BT709, range: Full
  extra data(59): 00 00 01 67 4D 00 29 8D 8D 40 3C 01 13 F2 CD C0 40 40 50 00 00 70 80 00 15 F9 00 40 00 00 00 00 00 00 01 68 EE 38 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 

stop recording
Format: mp4, range: 0 +0ms, bitrate: 0, size: 0
Metadata:
  encoder: Lavf61.1.100
Streams: 1
 Video:
  stream#0, range: 0 +0ms, frames: 0
  codec: h264 tag: 'avc1' profile: 77 level: 41, yuv420p, bpc:8, bpp:12(8,8,8), channels:(1,1,1), map: 0 1 2 0, bitrate: 0, 1920x1080, fps: 0, bframes: 0, primaries: BT709, trc: BT709, matrix: BT709, range: Full
  extra data(59): 00 00 01 67 4D 00 29 8D 8D 40 3C 01 13 F2 CD C0 40 40 50 00 00 70 80 00 15 F9 00 40 00 00 00 00 00 00 01 68 EE 38 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 

free custome avio: 000000001B9E8100
0000000018577280 close url: H:/codevideo/bin/video/2024-06-13-13-48-47-254.mp4
try to start to record

VideoFrame1703@21575.120886 to be destroyed is not rendered by  00000000182C6540
VideoFrame1712@21575.152992 to be destroyed is not rendered by  00000000182C6540
0000000018285110>8543 05:59:35.2/00:00:09 cache 3v 0.0s/0KB 12KB/s |+0|<4ms update 32.1ms 29.6fps draw 0/0 +67ms drop 2
VideoFrame1718@21575.200975 to be destroyed is not rendered by  00000000182C6540
0000000018364440 player.set(0)
0000000018285110 void __cdecl mdk::abi::MediaControlPush::setState(enum mdk::abi::State)@1068 requested state 1=>0, current state 1. status: 0X124
default FrameReader000000000CF59AB0 state: 1=>1=>0, 0
default000000000CF59AB0 mdk::abi::FrameReader::stop, current state: 1
000000000CF59AB0 mdk::abi::FrameReaderImpl::unload, current status 0x124, invalid: 0, loading: 0, loaded: 4, buffering: 0, seeking: 0, prepared: 256, EOF: 0
0000000018115918 request avformat input interrupt
wang-bin commented 5 months ago

无法正常保存是什么意思?看日志正常录制了

feiyangqingyun commented 5 months ago

录制的文件是一个固定大小的,没有任何视频内容

wang-bin commented 5 months ago

录制好日志里的视频信息 frames应该大于0,现在是0,不知道发生了什么。我有什么方法自己测吗?

feiyangqingyun commented 5 months ago

目前好像没有自测的办法,你方便的话可以远程给你看下现象。我打印了player->mediaInfo().video对应的frame_rate,是对的,帧率30

feiyangqingyun commented 5 months ago

无论录制多久,文件大小永远是261KB

https://github.com/wang-bin/mdk-sdk/assets/3894710/143e89e7-3d2f-4040-9a5a-66dacb82a824

wang-bin commented 5 months ago

ffprobe -show_packets -of csv 弄几秒日志看看

feiyangqingyun commented 5 months ago

ffprobe -f dshow video=Webcam -show_packets -of csv 默认用的是mjpeg采集,要指定格式才可以,而ffprobe不支持指定格式的命令,ffmpeg才支持。ffmpeg -f dshow -framerate 25 -video_size 640x480 -vcodec mjpeg -i video=Webcam 但是采集到的都是yuvj420p这个格式。 1.txt

feiyangqingyun commented 5 months ago

ffmpeg -f dshow -framerate 25 -video_size 640x480 -vcodec h264 -i video=Webcam

feiyangqingyun commented 5 months ago

感觉和yuvj420p这个格式关系不大,我找了个大华的网络摄像头,采集到的也是yuvj420p,用mdk能正常保存。一般H264的摄像头yuv420p格式居多

feiyangqingyun commented 5 months ago

找到了一个直接默认就是264的 2.txt

wang-bin commented 5 months ago

看上去是因为没检测到关键帧,我这边从关键帧才开始录的

feiyangqingyun commented 5 months ago

看来和之前的那个问题一样,之前这种264的摄像头是打不开,加了reader.starts_with_key=0,就可以。估计保存那边也判断下,=0就不判断也可以存储。

feiyangqingyun commented 5 months ago

名字都想好了,record.starts_with_key=0

wang-bin commented 5 months ago

名字都想好了,record.starts_with_key=0

不过我这边直接复用reader.starts_with_key=0最方便。你试试最新包

feiyangqingyun commented 5 months ago

试过了,可以,完美!现在就差一点了,就是麦克风的pcms16le数据在文件中,但是播放器好像都无法播放,可能需要转成pcma或者pcmu才行。

wang-bin commented 5 months ago

试过了,可以,完美!现在就差一点了,就是麦克风的pcms16le数据在文件中,但是播放器好像都无法播放,可能需要转成pcma或者pcmu才行。

发下日志或者录制的文件

feiyangqingyun commented 5 months ago

https://github.com/wang-bin/mdk-sdk/assets/3894710/9f4d1387-6881-4505-b8cb-435c31b50c81

打开录制后的文件.txt

feiyangqingyun commented 5 months ago

vlc提示

wang-bin commented 5 months ago

pcm没有声道信息,你发下从播放开始到录制结束的日志

feiyangqingyun commented 5 months ago

日志如下: 新建文本文档.txt

feiyangqingyun commented 5 months ago

我测试了用ffmpeg命令行录制264+pcm s16 到文件,mov格式,能够正常播放。说明行得通,mov是支持s16数据的 用vlc打开播放,显示如下的音频信息。 QQ截图20240616125510

feiyangqingyun commented 5 months ago

是不是要用avcodec_parameters_copy(streamOut->codecpar, streamIn->codecpar); 拷贝下信息比如音频的通道数等

wang-bin commented 5 months ago

ffmpeg声道用法变了,有些地方漏改了,最新包已修复

feiyangqingyun commented 5 months ago

是不是发布的包有延迟?我下载了,ffmpeg567都可以,能正常保存pcm到文件。并能播放。但是ffmpeg4还是不行。

wang-bin commented 5 months ago

4没试过,之前是所有版本都有问题