In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
1. Introduction
Video capture and compression are the steps that must be taken in the development of video applications such as video transmission and surveillance.
With regard to video capture, this paper discusses a simple and practical method based on Windows platform, which is realized by using VFW (Video For Windows) software package provided by Microsoft. It only needs a general USB camera to capture and save video conveniently.
In the aspect of video compression, this paper discusses how to use the open source encoder (T264) of H.264 standard, which has the highest compression ratio and the highest transmission reliability, to compress the captured video files. The video format captured by VFW is the AVI format without any compression. First, the AVI format is converted. Then use the T264 source code to compress and encode the converted video stream file, which greatly compresses the video file and facilitates the video transmission.
2. Collection of video information
Since the storage of video and audio data streams to AVI files can be easily realized by using VFW software package, the functions of VFW software package are encapsulated into AVICAP window class functions in Visual C++. Using AVICAP window class functions, programmers can capture, play and edit video clips by sending messages or setting properties, and can flexibly collect digital video signals from analog video sources. And store the captured video stream to disk or directly process the video cache.
The method described in this paper is implemented on Viusal C++ 6.0software platform, and the specific steps of software implementation are as follows:
1) before capturing video, you must first create a window for video capture and add some specific operation buttons. The window is created using the function capCreateCaptureWindow. If the window is created successfully, the handle of the window (hwndV in the program) is returned. If the creation is not successful, the NULL value is returned. The specific creation procedures and notes are as follows:
HwndV=capCreateCaptureWindow (
(LPSTR) "My Capture Window", / / capture window name
WS_CHILD | WS_VISIBLE, / / window style
150150300280, / / window location and size
(HWND) hwndMain, / / parent window handle
(int) 1); / / window identification
2) before the capture starts, associate the capture window with the video device. The VFW interface uses the function capDriverConnect (hWndCap,nIndex), where: the handle of the video capture window established by hWndCap; nIndex is the index number of the video card driver obtained by the query. Next, the ability and status information of the video capture equipment are obtained. In VFW, the function capDriverGetCaps (hwnd,psCaps,wsize) is used to obtain the capability of the acquisition equipment, and the capGetStatus (hwnd,s,size) function is used to obtain the status information of the acquisition equipment.
3) start the display mode and set its mode parameters. The AVICAP window class is implemented by two functions. The specific program and comments are as follows:
CapPreviewRate (hwndVideo, 66); / / set the preview playback rate
Capreview (hwndVideo, TRUE); / / start preview mode
4) capture and save the video stream, terminate the video capture and disconnect the connection with the capture device, and define a structure OPENFN at the beginning of the program to initialize a dialog box, which is used to save the video.
The specific procedures are as follows:
If (! isRecordFileOpen)
{
OPENFN ofname; / / Open the file structure
ZeroMemory (& ofname, sizeof (OPENFN)); / / initialize the structure
Ofname.lStructSize = sizeof (OPENFN); / / size of the structure
Ofname.hwndOwner = hwndMain; / / main window handle
Ofname.lpstrFile = recordFile; / / saved file pointer
Ofname.nMaxFile = sizeof (recordFile); / / Save the size of the file
Ofname.lpstrFilter = "Video\ 0*.avi"; / / Save the suffix of the file
Ofname.nFilterIndex = 1; / / File index number
Ofname.lpstrFileTitle = NULL; / / filename pointer
Ofname.nMaxFileTitle = 0
Ofname.lpstrInitialDir = NULL
Ofname.Flags=OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST
If (GetSaveFileName (& ofn) = = TRUE) / / displays the dialog box to save the file
{
Strcpy (recordFile, ofn.lpstrFile)
Strcat (recordFile, ".avi")
IsRecordFileOpen = true
}
}
After saving the file, you need to use the function CreateThread to create a recording thread to capture the video stream, and use the function capDriverDisconnect to terminate the video capture and disconnect the connection with the capture device. The specific code of the recording thread is as follows:
DWORD id; / / create a recording thread
SECURITY_ATTRIBUTES ma
Ma.nLength = sizeof (SECURITY_ATTRIBUTES)
Ma.lpSecurityDescriptor = NULL
Ma.bInheritHandle = TRUE
HVideoThread = & ma, (ULONG) 0
VideoThreadProc, (LPVOID) (ULONG) 0, (ULONG) 0, & id)
3. Conversion of video format
The video compression method based on H.264 coding standard is adopted in this paper, and the source code is t264 editor × × developed jointly by China Video coding Free Organization. Before using the source code, the encoder requires that the compressed file format should be YUV format video file, and the video file captured by VFW is the most original AVI format, so the format conversion should be carried out.
There is no direct formula for the conversion from AVI format to YUV format. If each frame of AVI video file stream corresponds to a BMP (RGB) file, you can use the formula to convert it into YUV file. The conversion formula is as follows:
Y = 0.299R + 0.587G + 0.114B
Cb = 0.564 (B-Y)
Cr = 0.713 (R-Y)
Among them, Cb corresponds to U _ _ (Cr) and corresponds to V, which represents the two components that make up the color, respectively, and in the program, it is realized by the following program (in 4:2:0 sampling format):
Void RGB2YUV (uint8 R, uint8 G, uint8 B, uint8 * y, uint8 * u, uint8 * v)
{
* y = Clip (66 * int (R) + 129 * int (G) + 25 * int (B) + 128) > > 8) + 16)
* u = Clip (- 38 * int (R)-74 * int (G) + 112 * int (B) + 128) > > 8) + 128)
* v = Clip (112 * int (R)-94 * int (G)-18 * int (B) + 128) > > 8) + 128)
}
4. Compression coding using encoder
The encoder jointly developed by the China Video Encoding Free Organization creates a console program based on the VisualC++ platform. The aforementioned video capture program is a windows program because it has windows, buttons and other view windows. In the VisualC++ platform, it is a very complex process to combine the console program with the windows program. You can use a C++ function ShellExecute to call the encoder function in the previous video capture program.
The specific implementation steps are as follows:
1) first, get the number of frames of the YUV file generated by format conversion after collection, and modify the number of encoded frames in the configuration file enconfig.txt. For example, if the number of frames is 100, change lines 6, 7 and 8 of the enconfig.txt file:
300 # total frame number
300 # i intervals
300 # idr intervals
Change to:
100 # total frame number
100 # i intervals
100 # idr intervals
After the number of frames is modified, the path generated by the file should also be changed to the path generated by the final exe file, which should be modified in the last three lines of enconfig.txt.
1) the CONSOLE program is used to encoder and encoder, and the exe program is usually executed on the command line. The method in this paper is to make use of the characteristics of the entry function main (int argc, char* argv []), where argc represents the number of parameters, while the argv [] array is used to save the pointer of the parameters, and the parameters are assigned in the program, thus eliminating the tedious process of entering the command line for execution. So as to generate an exe program that can be used directly. The specific assignment statements are as follows:
Argv [0] = "T264.exe"
Argv [1] = "- e"
Argv [2] = "enconfig.txt"
2) set the t264.exe file path, an executable t264.exe file has been generated in the above process. If the YUV file is placed in the same directory, the H.264 stream file can be generated when debugging and running the program to achieve the purpose of coding. However, this paper needs to complete the collection and compression coding in the same program, so copy the generated t264.exe file to the debug directory of the acquisition program.
3) after all the above work is done, you can call the encoder program in the acquisition program and use a function to implement it. The specific code is as follows:
ShellExecute (NULL, "open", "t264.exe"
"T264.exe-e enconfig.txt", NULL,SW_SHOWNORMAL)
Among them, SW_SHOWNORMAL refers to a program running style, which means that the program is running normally, and you can see the whole process of running the program (shown in the DOS command window) when calling. If you need the program to be executed transparently and invisible, you can use SW_HIDE to replace it.
At this point, the whole process of video capture, format conversion and coding compression is completed. The size of the AVI file collected in this paper is 4.075m, and the size of the YUV file after format conversion is 5.458m. However, the final size of the 264 bitstream is only 27k, and the compression ratio is 150.9 times.
5. Summary
The whole process from video capture, format conversion to compression coding discussed in this paper can be easily completed and can be directly applied to the pre-development of video surveillance or video network transmission. Bairui Technology has provided a solution for video surveillance, audio and video instant messaging. Http://www.bairuitech.com
If you have any questions about this blog, you can add 992139738.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 243
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.