Skip to content

Commit

Permalink
adds further details to videowriter_basic #65
Browse files Browse the repository at this point in the history
  • Loading branch information
mxochicale committed Mar 16, 2024
1 parent 032ef1c commit 07a0fa7
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 25 deletions.
18 changes: 0 additions & 18 deletions opencv/examples/cpp/README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,5 @@
# CPP Examples

## clean, build and execute
```
cd .. && rm -rf build && bash build.bash && cd build
./*APP
```

## execute
```
cd build
./*APP
vlc live.avi
```
## clean

```
cd .. && rm -rf build
```


## Reference
* Modern C++ Course: OpenCV 4 C++ in GNU/Linux (Tutorial 3, I. Vizzo, 2020)
Expand Down
3 changes: 1 addition & 2 deletions opencv/examples/cpp/videowriter_basic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ set(${PROJECT_NAME}_SRC
find_package(OpenCV 4 REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})

#########################################################
# Executable
#Executable
add_executable( ${PROJECT_NAME} ${${PROJECT_NAME}_SRC} )
target_link_libraries( ${PROJECT_NAME} ${OpenCV_LIBS} )

59 changes: 59 additions & 0 deletions opencv/examples/cpp/videowriter_basic/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,65 @@
# Video Recording

## video features
```
v4l2-ctl --list-formats-ext
ioctl: VIDIOC_ENUM_FMT
Type: Video Capture
[0]: 'MJPG' (Motion-JPEG, compressed)
Size: Discrete 1280x720
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 960x540
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 848x480
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 640x480
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 640x360
Interval: Discrete 0.033s (30.000 fps)
[1]: 'YUYV' (YUYV 4:2:2)
Size: Discrete 640x480
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 640x360
Interval: Discrete 0.033s (30.000 fps)
```

v4l2-ctl --all
```
...
Video input : 0 (Input 1: ok)
Format Video Capture:
Width/Height : 640/480
Pixel Format : 'YUYV' (YUYV 4:2:2)
Field : None
Bytes per Line : 1280
Size Image : 614400
Colorspace : sRGB
Transfer Function : Rec. 709
```


## clean, build and execute
```
bash build.bash
```


## execute
```
./build/videowriter
vlc live.avi
```

## clean
```
rm -rf build live.avi
```


## Reference
https://docs.opencv.org/4.9.0/df/d94/samples_2cpp_2videowriter_basic_8cpp-example.html#a5
https://github.com/Myzhar/simple-opencv-kalman-tracker
https://www.myzhar.com/blog/tutorials/tutorial-opencv-ball-tracker-using-kalman-filter/
https://www.youtube.com/watch?v=sG-h5ONsj9s&feature=emb_title
Expand Down
1 change: 1 addition & 0 deletions opencv/examples/cpp/videowriter_basic/build.bash
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
## how to compile
rm -rf build
mkdir build && cd build
cmake ..
make
Expand Down
24 changes: 20 additions & 4 deletions opencv/examples/cpp/videowriter_basic/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,46 @@
#include <stdio.h>
using namespace cv;
using namespace std;

#define VID_WIDTH 640
#define VID_HEIGHT 480
#define VIDEO_IN "/dev/video0"

int main(int, char**)
{
Mat src;
// use default camera as video source
VideoCapture cap(0);
VideoCapture cap(VIDEO_IN);
// check if we succeeded
if (!cap.isOpened()) {
cerr << "ERROR! Unable to open camera\n";
return -1;
}

// get one frame from camera to know frame size and type
cap >> src;
// check if we succeeded
if (src.empty()) {
cerr << "ERROR! blank frame grabbed\n";
return -1;
}

cout << "Original video size " << src.size() << endl;
Size targetSize(VID_WIDTH, VID_HEIGHT);
cap.set(cv::CAP_PROP_FRAME_WIDTH, VID_WIDTH);
cap.set(cv::CAP_PROP_FRAME_HEIGHT, VID_HEIGHT);
cout << "Target video size " << targetSize << endl;

cout << "Source type " << src.type() << endl;
bool isColor = (src.type() == CV_8UC3);

//--- INITIALIZE VIDEOWRITER
VideoWriter writer;
int codec = VideoWriter::fourcc('M', 'J', 'P', 'G'); // select desired codec (must be available at runtime)
double fps = 25.0; // framerate of the created video stream
double fps = 30.0; // framerate of the created video stream
string filename = "./live.avi"; // name of the output video file
writer.open(filename, codec, fps, src.size(), isColor);
//writer.open(filename, codec, fps, src.size(), isColor);
writer.open(filename, codec, fps, targetSize, isColor);
// check if we succeeded
if (!writer.isOpened()) {
cerr << "Could not open the output video file for write\n";
Expand All @@ -47,7 +63,7 @@ int main(int, char**)
// encode the frame into the videofile stream
writer.write(src);
// show live and wait for a key with timeout long enough to show images
imshow("Live", src);
imshow("Live Video Stream", src);
if (waitKey(5) >= 0)
break;
}
Expand Down
2 changes: 1 addition & 1 deletion opencv/installation/installation.bash
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ unzip opencv_contrib.zip
# Create build directory and switch into it
mkdir -p build && cd build
# Configure
cmake -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib-4.x/modules ../opencv-4.x
cmake -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib-4.x/modules ../opencv-4.x -DWITH_FFMPEG=ON -DOPENCV_GENERATE_PKGCONFIG=ON

### leaving the folling lines for reference
#cmake -D CMAKE_BUILD_TYPE=RELEASE \
Expand Down

0 comments on commit 07a0fa7

Please sign in to comment.