c Add wave music to the silent avi video (it is estimated that the avi audio and video is actually written specifically for the entire network)

During programming, I found that many parameters in the header file structure have very loose requirements, and only a few are important.

In fact, this program is to prepare for later recording with a camera and microphone. I recorded the audio used in this program with a microphone.

Now the avi generated by this program can be played by deepin’s built-in video playback software.

I understand that the playback time of video and audio intersecting data must be equal per unit time. Audio and video can be crossed multiple times within the unit, or they can be crossed once.

Each piece of intersecting audio and video data must be preceded by data type identifiers xxdc, xxwb and data length. Tell the player what type of data below it is and how longit is.

.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <malloc.h>
#include <wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>

static int ap;
static int at;
static int nframe = 0;

static unsigned int vzj = 0;
static unsigned int azj = 0;

//The following 7 parameters must be defined first
#define zwidth 720
#define zheigth 496
#define zvrate 30 //video frame rate
#define channal 2 //audio channel number
#define zarate 44100 //audio sampling rate
#define bitper 16 //audio sampling width
#define vbitper 24 //video sampling width


int main(void) {

struct file {
struct riff {
char id[4];
unsigned int size;
char type[4];

struct hdrl {

char id[4];
unsigned int size;
unsigned char type[4];

struct avih {
unsigned char id[4]; //Block ID, fixed to avih
unsigned int size; //Block size, equal to the size of struct avi_avih_chunk minus id and size
unsigned int us_per_frame; //Video frame interval time (in microseconds)
unsigned int max_bytes_per_sec; //Maximum data rate of AVI file
unsigned int padding; //Set to 0
unsigned int flags; //AVI file global attributes, such as whether it contains index blocks, whether audio and video data are interleaved, etc.
unsigned int total_frames; //Total number of frames
unsigned int init_frames; //Specify the initial number of frames for interactive format (non-interactive format should be specified as 0)
unsigned int streams; //The number of streams contained in the file, 1 when there is only a video stream
unsigned int suggest_buff_size; //Specify the buffer size recommended for reading this file, usually to store a frame of image
unsigned int width; //Width of main video window (unit: pixels)
unsigned int height; //Height of main video window (unit: pixels)
unsigned int reserved[4]; //Reserved segment, set to 0

} av;

struct strl {
unsigned char id[4]; //Block ID, fixed to LIST
unsigned int size; //Block size, equal to the size of struct avi_strl_list minus id and size
unsigned char type[4]; //strl

struct strh1 {
unsigned char id[4]; //Block ID, fixed to strh
unsigned int size; //Block size, equal to the size of struct avi_strh_chunk minus id and size
unsigned char stream_type[4]; //Type of stream, vids represents video stream, auds represents audio stream
unsigned char codec[4]; //Specify the decoder required to process this stream, such as JPEG
unsigned int flags; //Flags, such as whether to allow this stream to output, whether the palette changes, etc., generally set to 0
unsigned short priority; //The priority of the stream, just set the video stream to 0
unsigned short language; //Audio language code, video stream can be set to 0
unsigned int init_frames; //Specify the initial number of frames for interactive format (non-interactive format should be specified as 0)
unsigned int scale; //
unsigned int rate; //For video streaming, rate / scale = frame rate fps
unsigned int start; //For video streaming, set it to 0
unsigned int length; //For video streams, length is the total number of frames
unsigned int suggest_buff_size; //The recommended buffer size for reading this stream data
unsigned int quality; //quality indicator of streaming data
unsigned int sample_size; //Audio sample size, video stream can be set to 0
short left; //The display position of this stream in the main video window can be set to {0,0,width,height}
short top;
short right;
short bottom;
} sh1;
struct strf1 {
unsigned char id[4]; //Block ID, fixed to strf
unsigned int size; //Block size, equal to the size of struct avi_strf_chunk minus id and size
//_BITMAPINFOHEADER structure
int size1; // Specifies the number of bytes required for the structure. =size
unsigned int width; //Specify the width of the bitmap (in pixels).
unsigned int height; //Specify the height of the bitmap (in pixels)
unsigned short planes; //Specify the number of planes of the target device. This value must be set to 1.
unsigned short bitcount; //The number of bits occupied by each pixel can only be one of 1, 4, 8, 16, 24 and 32
unsigned char compression[4]; //Specify the compression type of the compressed bottom-up bitmap
unsigned int image_size; //Specify the size of the image (in bytes), equal to width * height * bitcount / 8
unsigned int x_pixels_per_meter; //The horizontal resolution of the display device, just set it to 0
unsigned int y_pixels_per_meter; //Vertical resolution of the display device, just set it to 0
unsigned int num_colors; //The meaning is unclear, just set it to 0
unsigned int imp_colors; //The meaning is unclear, just set it to 0
} sf1;
} sl1;

struct strl2 {
unsigned char id[4]; //Block ID, fixed to LIST
unsigned int size; //Block size, equal to the size of struct avi_strl_list minus id and size
unsigned char type[4]; //strl

struct strh2 {
unsigned char id[4]; //Block ID, fixed to strh
unsigned int size; //Block size, equal to the size of struct avi_strh_chunk minus id and size
unsigned char stream_type[4]; //Type of stream, vids represents video stream, auds represents audio stream
unsigned char codec[4]; //Specify the decoder required to process this stream, such as JPEG
unsigned int flags; //Flags, such as whether to allow this stream to output, whether the palette changes, etc., generally set to 0
unsigned short priority; //The priority of the stream, just set the video stream to 0
unsigned short language; //Audio language code, video stream can be set to 0
unsigned int init_frames; //Specify the initial number of frames for interactive format (non-interactive format should be specified as 0)
unsigned int scale; //
unsigned int rate; //For video streaming, rate / scale = frame rate fps
unsigned int start; //For video streaming, set it to 0
unsigned int length; //For video streams, length is the total number of frames
unsigned int suggest_buff_size; //The recommended buffer size for reading this stream data
unsigned int quality; //quality indicator of streaming data
unsigned int sample_size; //Audio sample size, video stream can be set to 0
short left; //The display position of this stream in the main video window can be set to {0,0,width,height}
short top;
short right;
short bottom;
} sh2;
struct strf2 {
char id[4]; //strf
int size;
short wFormatTag; //Waveform audio format type, format tag registered in Microsoft Corporation, used for many compression algorithms,
short nChannels; //number of channels 85= WAVE_FORMAT_PCM
int nSamplesPerSec; //Sampling rate, in samples per second (Hz), common values are 8.0 kHz, 11.025 kHz, 22.05 kHz and 44.1 kHz
int nAvgBytesPerSec; //Average data transfer rate required for format tags (in bytes/second) =nsam*nblock
short nBlockAlign; //Block alignment (in bytes), nBlockAlign must be equal to the product of nChannels and wBitsPerSample divided by 8 (bits/byte)
short wBitsPerSample; // If wFormatTag WAVE_FORMAT_PCM, wBitsPerSample should be equal to 8 or 16
short biSize; //The size (in bytes) of the additional format information appended to the end of the WAVEFORMATEX structure
} sf2;
} sl2;
} hd;

struct movi {
char id[4];
unsigned int size;
char type[4];
}mo;

}ri;

} head;



FILE *fo = fopen("/home/wjs/Videos/demo.avi", "w + b"); //Generate avi
FILE *f = fopen("/home/wjs/Videos/tra_mjpg.avi", "r + b"); //Silent video file
FILE *af = fopen("/home/wjs/Music/15s.wav", "r + b"); //wav file
fseek(fo, sizeof(head), SEEK_SET);


if (f == NULL) {
puts("file_in error");
exit(-1);
}
if (af == NULL) {
puts("file2_in error");
exit(-1);
}
fseek(f, 0, SEEK_END);
int flen = ftell(f);
fseek(f, 0, SEEK_SET);
fseek(af, 0, SEEK_END);
int aflen = ftell(af);
fseek(af, 0, SEEK_SET);

int tf = fileno(f);
int taf = fileno(af);

char *mf = mmap(NULL, flen, PROT_READ | PROT_WRITE, MAP_SHARED, tf, 0);
char *maf = mmap(NULL, aflen, PROT_READ | PROT_WRITE, MAP_SHARED, taf, 0);

for (int a = 0; a < aflen; a + + ) {
if ((maf[a] == 'd') & amp; & amp; (maf[a + 1] == 'a') & amp; & amp; (maf[a + 2] == 't' ) & amp; & amp; (maf[a + 3] == 'a')) {
ap = a + 4 + 4;
at = ap;

}
}



int t = 0;
while (t < flen) {
int n = 0;
while ((n <zvrate) & amp; & amp; (t < flen)) {

if ((mf[t] == '0') & amp; & amp; (mf[t + 1] == '0') & amp; & amp; (mf[t + 2] == 'd' ) & amp; & amp; (mf[t + 3]) == 'c') {
char c00dc[] = {'0', '0', 'd', 'c'};
fwrite(c00dc, 4, 1, fo);

fwrite( & amp;mf[t + 4], 4, 1, fo); //write length
int dx = 0;
memcpy( & amp;dx, & amp;mf[t + 4], 4);
fwrite( & amp;mf[t + 8], dx, 1, fo); //Write data

vzj = vzj + 8 + dx;
n + + ;
t + + ;
nframe + + ;
} else {
t + + ;
}

}

//------------------------ In units of one second, the video and audio data are crossed once ---------- ----------------------------------

char c01wb[] = {'0', '1', 'w', 'b'};
fwrite(c01wb, 4, 1, fo);
            
int h=channal*zarate*bitper/8;
fwrite( & amp;h,4,1,fo);
\t\t 
azj = azj + 8;

fwrite( & amp;maf[ap],h, 1, fo); //Write audio data
ap = ap + h;
azj = azj + h;
        
}


//------------------------------------------------
typedef struct file File;
File ffile = {
{
//riff
{'R', 'I', 'F', 'F'},
vzj + azj + 8,
{'A', 'V', 'I', ' '},

//hdrl
{
{'L', 'I', 'S', 'T'},
sizeof(head.ri.hd) - 8,
{'s', 't', 'r', 'l'},
//avih
{
{'a', 'v', 'i', 'h'},
sizeof(head.ri.hd.av) - 8,
1000000/zvrate,
zwidth*zheigth*vbitper*zvrate,
0, 0, nframe,0,
2, //Total number of audio + video streams
zwidth*zheigth*vbitper,
zwidth, zheigth, {0, 0, 0, 0}

},
//strl1
{
{'L', 'I', 'S', 'T'},
sizeof(head.ri.hd.sl1) - 8,
{'s', 't', 'r', 'l'},
{
//strh1
{'s', 't', 'r', 'h'},
sizeof(head.ri.hd.sl1.sh1) - 8,
{'v', 'i', 'd', 's'}, //Represents video
{'M', 'J', 'P', 'G'}, //Image format
0, 0, 0, 0,
1,zvrate,
0, nframe,
zwidth*zheigth*vbitper*zvrate,
10000, 0,
0, 0,zwidth,zheigth
},
{
//strf1
{'s', 't', 'r', 'f'},
sizeof(head.ri.hd.sl1.sf1) - 8,
sizeof(head.ri.hd.sl1.sf1) - 8,
zwidth, zheigth, 1,
vbitper,
{'M', 'J', 'P', 'G'}, //Image format
zwidth*zheigth*vbitper/8,
0, 0, 0, 0
}
},
//strl2
{
{'L', 'I', 'S', 'T'},
sizeof(head.ri.hd.sl2) - 8,

{'s', 't', 'r', 'l'},
{
//strh2
{'s', 't', 'r', 'h'},
sizeof(head.ri.hd.sl2.sh2) - 8,
{'a', 'u', 'd', 's'}, //represents sound
{0, 0, 0, 0},
0, 0, 0, 0,
1,zarate, //This frame rate will affect the frame rate of the image
0, 0,channel*zarate*bitper/8, 10000,zarate,
0, 0, 0, 0

},
{
//strf2

{'s', 't', 'r', 'f'},
sizeof(head.ri.hd.sl2.sf2) - 8,
1,channal,zarate,zarate*channal*bitper/8,channal*bitper/8,bitper, 0

}
}
},

//movi
{
{'L', 'I', 'S', 'T'},
// movisize,
vzj,
{'m', 'o', 'v', 'i'}
}

}
};
fseek(fo, 0, SEEK_SET);
fwrite( & amp;ffile, sizeof(ffile), 1, fo);

//----------------------------------------

fclose(fo);
puts("over");
return 0;
}

Attached is to view the avi file parameters:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <malloc.h>
#include <wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>


int main(void){
\t
struct file{
struct riff{
char id[4];
unsigned int size;
char type[4];
\t\t\t
struct hdrl{
\t\t\t\t
char id[4];
unsigned int size;
unsigned char type[4];
\t\t\t\t
struct avih{
unsigned char id[4]; //Block ID, fixed to avih
unsigned int size; //Block size, equal to the size of struct avi_avih_chunk minus id and size
unsigned int us_per_frame; //Video frame interval time (in microseconds)
unsigned int max_bytes_per_sec; //Maximum data rate of AVI file
unsigned int padding; //Set to 0
unsigned int flags; //AVI file global attributes, such as whether it contains index blocks, whether audio and video data are interleaved, etc.
unsigned int total_frames; //Total number of frames
unsigned int init_frames; //Specify the initial number of frames for interactive format (non-interactive format should be specified as 0)
unsigned int streams; //The number of streams contained in the file, 1 when there is only a video stream
unsigned int suggest_buff_size; //Specify the buffer size recommended for reading this file, usually to store a frame of image
unsigned int width; //Width of main video window (unit: pixels)
unsigned int height; //Height of main video window (unit: pixels)
unsigned int reserved[4]; //Reserved segment, set to 0
\t\t\t\t\t
}av;
\t\t\t\t
struct strl{
unsigned char id[4]; //Block ID, fixed to LIST
unsigned int size; //Block size, equal to the size of struct avi_strl_list minus id and size
unsigned char type[4]; //strl
\t\t\t\t\t
struct strh1{
unsigned char id[4]; //Block ID, fixed to strh
unsigned int size; //Block size, equal to the size of struct avi_strh_chunk minus id and size
unsigned char stream_type[4]; //Type of stream, vids represents video stream, auds represents audio stream
unsigned char codec[4]; //Specify the decoder required to process this stream, such as JPEG
unsigned int flags; //Flags, such as whether to allow this stream to output, whether the palette changes, etc., generally set to 0
unsigned short priority; //The priority of the stream, just set the video stream to 0
unsigned short language; //Audio language code, video stream can be set to 0
unsigned int init_frames; //Specify the initial number of frames for interactive format (non-interactive format should be specified as 0)
unsigned int scale; //
unsigned int rate; //For video streaming, rate / scale = frame rate fps
unsigned int start; //For video streaming, set it to 0
unsigned int length; //For video streams, length is the total number of frames
unsigned int suggest_buff_size; //The recommended buffer size for reading this stream data
unsigned int quality; //quality indicator of streaming data
unsigned int sample_size; //Audio sample size, video stream can be set to 0
short left; //The display position of this stream in the main video window can be set to {0,0,width,height}
short top;
short right;
short bottom;
}sh1;
struct strf1{
unsigned char id[4]; //Block ID, fixed to strf
unsigned int size; //Block size, equal to the size of struct avi_strf_chunk minus id and size
//_BITMAPINFOHEADER structure
int size1; // Specifies the number of bytes required for the structure. =size
unsigned int width; //Specify the width of the bitmap (in pixels).
unsigned int height; //Specify the height of the bitmap (in pixels)
unsigned short planes; //Specify the number of planes of the target device. This value must be set to 1.
unsigned short bitcount; //The number of bits occupied by each pixel can only be one of 1, 4, 8, 16, 24 and 32
unsigned char compression[4]; //Specify the compression type of the compressed bottom-up bitmap
unsigned int image_size; //Specify the size of the image (in bytes), equal to width * height * bitcount / 8
unsigned int x_pixels_per_meter; //The horizontal resolution of the display device, just set it to 0
unsigned int y_pixels_per_meter; //Vertical resolution of the display device, just set it to 0
unsigned int num_colors; //The meaning is unclear, just set it to 0
unsigned int imp_colors; //The meaning is unclear, just set it to 0
}sf1;
}sl1;
\t\t\t\t
struct strl2{
unsigned char id[4]; //Block ID, fixed to LIST
unsigned int size; //Block size, equal to the size of struct avi_strl_list minus id and size
unsigned char type[4]; //strl
\t\t\t\t\t
struct strh2{
unsigned char id[4]; //Block ID, fixed to strh
unsigned int size; //Block size, equal to struct avi_strh_chunk minus id and size? Small
unsigned char stream_type[4]; //Type of stream, vids represents video stream, auds represents audio stream
unsigned char codec[4]; //Specify the decoder required to process this stream, such as JPEG
unsigned int flags; //Flags, such as whether to allow this stream to output, whether the palette changes, etc., generally set to 0
unsigned short priority; //The priority of the stream, just set the video stream to 0
unsigned short language; //Audio language code, video stream can be set to 0
unsigned int init_frames; //Specify the initial number of frames for interactive format (non-interactive format should be specified as 0)
unsigned int scale; //
unsigned int rate; //For video streaming, rate / scale = frame rate fps
unsigned int start; //For video streaming, set it to 0
unsigned int length; //For video streams, length is the total number of frames
unsigned int suggest_buff_size; //The recommended buffer size for reading this stream data
unsigned int quality; //quality indicator of streaming data
unsigned int sample_size; //Audio sample size, video stream can be set to 0
short left; //The display position of this stream in the main video window can be set to {0,0,width,height}
short top;
short right;
short bottom;
}sh2;
struct strf2{
char id[4]; //strf
int size;
short wFormatTag; //Waveform audio format type, format tag registered in Microsoft Corporation, used for many compression algorithms,
short nChannels; //number of channels 85= WAVE_FORMAT_PCM
int nSamplesPerSec; //Sampling rate, in samples per second (Hz), common values are 8.0 kHz, 11.025 kHz, 22.05 kHz and 44.1 kHz
int nAvgBytesPerSec; //Average data transfer rate required for format tags (in bytes/second) =nsam*nblock
short nBlockAlign; //Block alignment (in bytes), nBlockAlign must be equal to the product of nChannels and wBitsPerSample divided by 8 (bits/byte)
short wBitsPerSample; // If wFormatTag WAVE_FORMAT_PCM, wBitsPerSample should be equal to 8 or 16
short biSize; //The size (in bytes) of the additional format information appended to the end of the WAVEFORMATEX structure
}sf2;
}sl2;
}hd;
\t\t\t
struct movi{
char id[4];
unsigned int size;
char type[4];
}mo;
\t\t\t
}ri;
\t\t
}head;

FILE *f=fopen("/home/wzpc/Videos/2vuy.avi","rb");
if(f==NULL){
puts("file in error");
exit(-1);
}
fseek(f,0,SEEK_END);
int len=ftell(f);
fseek(f,0,SEEK_SET);
int fd=fileno(f);
char *m=mmap(NULL,len,PROT_READ,MAP_SHARED,fd,0);
\t
memcpy( & amp;head, & amp;m[0],sizeof(head));
//avih
printf("-----------avih--------------\
");
printf("avih_id: %s\
",head.ri.hd.av.id);
printf("size: %d\
",head.ri.hd.av.size);
printf("us_per_frame: %d\
",head.ri.hd.av.us_per_frame);
printf("max_bytes_per_sec: %d\
",head.ri.hd.av.max_bytes_per_sec);
printf("paddind: %d\
",head.ri.hd.av.padding);
printf("flags: %d\
",head.ri.hd.av.flags);
printf("total_frames: %d\
",head.ri.hd.av.total_frames);
printf("init_frame: %d\
",head.ri.hd.av.init_frames);
printf("stream: %d\
",head.ri.hd.av.streams);
printf("suggest_buff_size: %d\
",head.ri.hd.av.suggest_buff_size);
printf("width: %d\
",head.ri.hd.av.width);
printf("height: %d\
",head.ri.hd.av.height);
\t
//strh
printf("----------video strh--------------\
");
printf("strh_id: %s\
",head.ri.hd.sl1.sh1.id);
printf("size: %d\
",head.ri.hd.sl1.sh1.size);
printf("stream_type: %s\
",head.ri.hd.sl1.sh1.stream_type);
printf("codes: %s\
",head.ri.hd.sl1.sh1.codec);
printf("flags: %d\
",head.ri.hd.sl1.sh1.flags);
printf("priority: %d\
",head.ri.hd.sl1.sh1.priority);
printf("language: %d\
",head.ri.hd.sl1.sh1.language);
printf("init_frame: %d\
",head.ri.hd.sl1.sh1.init_frames);
printf("scale: %d\
",head.ri.hd.sl1.sh1.scale);
printf("rate: %d\
",head.ri.hd.sl1.sh1.rate);
printf("start: %d\
",head.ri.hd.sl1.sh1.start);
printf("length: %d\
",head.ri.hd.sl1.sh1.length);
printf("suggest_buff_size: %d\
",head.ri.hd.sl1.sh1.suggest_buff_size);
printf("quality: %d\
",head.ri.hd.sl1.sh1.quality);
printf("sample_size: %d\
",head.ri.hd.sl1.sh1.sample_size);
\t
printf("----------audio strh--------------\
");
printf("ausio strh_id: %s\
",head.ri.hd.sl2.sh2.id);
printf("size: %d\
",head.ri.hd.sl2.sh2.size);
printf("stream_type: %s\
",head.ri.hd.sl2.sh2.stream_type);
printf("codes: %s\
",head.ri.hd.sl2.sh2.codec);
printf("flags: %d\
",head.ri.hd.sl2.sh2.flags);
printf("priority: %d\
",head.ri.hd.sl2.sh2.priority);
printf("language: %d\
",head.ri.hd.sl2.sh2.language);
printf("init_frame: %d\
",head.ri.hd.sl2.sh2.init_frames);
printf("scale: %d\
",head.ri.hd.sl2.sh2.scale);
printf("rate: %d\
",head.ri.hd.sl2.sh2.rate);
printf("start: %d\
",head.ri.hd.sl2.sh2.start);
printf("length: %d\
",head.ri.hd.sl2.sh2.length);
printf("suggest_buff_size: %d\
",head.ri.hd.sl2.sh2.suggest_buff_size);
printf("quality: %d\
",head.ri.hd.sl2.sh2.quality);
printf("sample_size: %d\
",head.ri.hd.sl2.sh2.sample_size);
printf("----------audio strf--------------\
");
printf("audio strf:id %s\
",head.ri.hd.sl2.sf2.id);
printf("size: %d\
",head.ri.hd.sl2.sf2.size);
printf("Wave format: %d\
",head.ri.hd.sl2.sf2.wFormatTag);
printf("nChannls: %d\
",head.ri.hd.sl2.sf2.nChannels);
printf("Sampling rate: %d\
",head.ri.hd.sl2.sf2.nSamplesPerSec);
printf("Average rate: %d\
",head.ri.hd.sl2.sf2.nAvgBytesPerSec);
printf("nMlockAlign: %d\
",head.ri.hd.sl2.sf2.nBlockAlign);
printf("Sampling bit width: %d\
",head.ri.hd.sl2.sf2.wBitsPerSample);
printf("biSize: %d\
",head.ri.hd.sl2.sf2.biSize);
\t
return 0;
}

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. C Skill Tree Home Page Overview 194006 people are learning the system