5 #include "mpeg3private.inc"
7 void copy_data(FILE *out
, FILE *in
, long bytes
)
9 long fragment_size
= 0x100000;
10 char *buffer
= malloc(fragment_size
);
13 for(i
= 0; i
< bytes
; i
+= fragment_size
)
15 if(i
+ fragment_size
> bytes
) fragment_size
= bytes
- i
;
16 if(!fread(buffer
, 1, fragment_size
, in
))
20 if(!fwrite(buffer
, 1, fragment_size
, out
))
29 void split_video(char *path
, long default_fragment_size
)
31 long current_byte
= 0;
35 FILE *in
= fopen(path
, "r");
37 long sequence_hdr_size
;
38 unsigned long header
= 0;
47 fseek(in
, 0, SEEK_END
);
48 total_bytes
= ftell(in
);
49 fseek(in
, 0, SEEK_SET
);
51 // Copy sequence header
54 header
= (header
& 0xffffffff) | getc(in
);
55 }while(header
!= MPEG3_SEQUENCE_START_CODE
&& !feof(in
));
57 header_start
= ftell(in
) - 4;
60 header
= (header
& 0xffffffff) | getc(in
);
61 }while(header
!= MPEG3_GOP_START_CODE
&& !feof(in
));
62 header_end
= ftell(in
) - 4;
64 sequence_hdr_size
= header_end
- header_start
;
65 sequence_hdr
= malloc(sequence_hdr_size
);
66 fseek(in
, header_start
, SEEK_SET
);
67 fread(sequence_hdr
, 1, sequence_hdr_size
, in
);
68 fseek(in
, 0, SEEK_SET
);
70 while(current_byte
< total_bytes
&& !result
)
76 sprintf(outpath
, "%s%02d", path
, i
);
77 out
= fopen(outpath
, "w");
84 // Default fragment size
85 fragment_size
= default_fragment_size
;
86 // Corrected fragment size
87 if(current_byte
+ fragment_size
>= total_bytes
)
89 fragment_size
= total_bytes
- current_byte
;
96 fseek(in
, current_byte
+ fragment_size
, SEEK_SET
);
99 fseek(in
, -1, SEEK_CUR
);
101 header
|= ((unsigned long)fgetc(in
)) << 24;
102 fseek(in
, -1, SEEK_CUR
);
103 }while(ftell(in
) > 0 && header
!= MPEG3_GOP_START_CODE
);
104 fragment_size
= ftell(in
) - current_byte
;
105 fseek(in
, current_byte
, SEEK_SET
);
108 // Put sequence header
111 fwrite(sequence_hdr
, 1, sequence_hdr_size
, out
);
115 copy_data(out
, in
, fragment_size
);
119 current_byte
+= fragment_size
;
124 int main(int argc
, char *argv
[])
131 fprintf(stderr
, "Split elementary streams into chunks of bytes.\n"
132 "Usage: mpeg3split -b bytes <infile>\n");
136 for(i
= 1; i
< argc
; i
++)
138 if(!strcmp(argv
[i
], "-b"))
143 bytes
= atol(argv
[i
]);
147 fprintf(stderr
, "-b must be paired with a value\n");
155 split_video(argv
[i
], bytes
);
159 fprintf(stderr
, "No value for bytes specified,\n");