gocv
gocv copied to clipboard
use VideoWriterFile to save video file but do not save stream
Description
I want to save video stream as a file.I use OpenVideoCapture to get camera stream and use VideoWriter to save .
Steps to Reproduce
func main() {
// set to use a video capture device 0
deviceID := 0
// open webcam
webcam, err := gocv.OpenVideoCapture(deviceID)
if err != nil {
fmt.Println(err)
return
}
defer webcam.Close()
// open display window
window := gocv.NewWindow("Test")
defer window.Close()
// prepare image matrix
img := gocv.NewMat()
defer img.Close()
fmt.Printf("start reading camera device: %v\n", deviceID)
fps := 25.0
filename := "./live.avi"
var writer *gocv.VideoWriter
isColor := true
codec := "h264"
//fmt.Println("%d", img.Size()[0])
writer, err = gocv.VideoWriterFile(filename, codec, float64(fps), 720, 1280, isColor)
defer writer.Close()
if err != nil {
fmt.Println(err)
return
}
if !writer.IsOpened() {
fmt.Println("Could not open the output video file for write")
return
}
for {
if ok := webcam.Read(&img); !ok {
fmt.Printf("cannot read device %v\n", deviceID)
return
}
if img.Empty() {
continue
}
err := writer.Write(img)
if err != nil {
fmt.Println(err)
return
}
// show the image in the window, and wait 1 millisecond
window.IMShow(img)
if window.WaitKey(5) > 0 {
break
}
}
return
}
there was not any error my compiler reported.But the live.avi file do not play and no stream data. use ffmpeg command to get live.avi info.
ffprobe -v 0 ./live.avi -print_format json -show_format -show_streams
{
"streams": [
{
"index": 0,
"codec_name": "h264",
"codec_long_name": "H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10",
"codec_type": "video",
"codec_time_base": "1/50",
"codec_tag_string": "h264",
"codec_tag": "0x34363268",
"width": 720,
"height": 1280,
"coded_width": 720,
"coded_height": 1280,
"has_b_frames": 0,
"level": -99,
"chroma_location": "left",
"refs": 1,
"is_avc": "false",
"nal_length_size": "0",
"r_frame_rate": "25/1",
"avg_frame_rate": "25/1",
"time_base": "1/25",
"start_pts": 0,
"start_time": "0.000000",
"duration_ts": 0,
"duration": "0.000000",
"disposition": {
"default": 0,
"dub": 0,
"original": 0,
"comment": 0,
"lyrics": 0,
"karaoke": 0,
"forced": 0,
"hearing_impaired": 0,
"visual_impaired": 0,
"clean_effects": 0,
"attached_pic": 0,
"timed_thumbnails": 0
}
}
],
"format": {
"filename": "./live.avi",
"nb_streams": 1,
"nb_programs": 0,
"format_name": "avi",
"format_long_name": "AVI (Audio Video Interleaved)",
"start_time": "0.000000",
"size": "5686",
"probe_score": 100,
"tags": {
"encoder": "Lavf58.29.100"
}
}
}
and play it
ffplay ./live.avi
[avi @ 0x7ff1f186e600] Could not find codec parameters for stream 0 (Video: h264 (h264 / 0x34363268), none, 720x1280): unspecified pixel format
Consider increasing the value for the 'analyzeduration' and 'probesize' options
Input #0, avi, from './live.avi':
Metadata:
encoder : Lavf58.29.100
Duration: N/A, start: 0.000000, bitrate: N/A
Stream #0:0: Video: h264 (h264 / 0x34363268), none, 720x1280, 25 fps, 25 tbr, 25 tbn, 50 tbc
nan M-V: nan fd= 0 aq= 0KB vq= 0KB sq= 0B f=0/0
Your Environment
my environment is: gocv version: 0.23.0 opencv lib version: 4.3.0 macOs10 I use brew install opencv to install OpenCV
What happen for my code ? what should I do?
The same problem, we want to cast a video stream by rtmp or rtsp like opencv, any suggenstions?
Having the same problem here
I was having this problem too and then I realized that the resolution of the image should be the same as the VideoWriterFile resolution.
writer, err := gocv.VideoWriterFile("video.mp4", "avc1", float64(25), 1280, 720, true)
img := gocv.IMRead("/path/to/image", gocv.IMReadColor)
resized := gocv.NewMat()
gocv.Resize(img, &resized, image.Pt(1280, 720), 0, 0, gocv.InterpolationArea)
writer.Write(resized)
writer.Close()
Actually the library didn't warn me, it would be nice if an improvement was made about it.