mp4 文件 stco 解析

参考文档 14496-12

ISO_IEC_14496-12_2015

8.7.5
8.7.5.1
Chunk Offset Box
Definition
Box Type: ‘stco’, ‘co64’
Container: Sample Table Box (‘stbl’)
Mandatory: Yes
Quantity: Exactly one variant must be present
The chunk offset table gives the index of each chunk into the containing file. There are two variants,
permitting the use of 32‐bit or 64‐bit offsets. The latter is useful when managing very large
presentations. At most one of these variants will occur in any single instance of a sample table.
Offsets are file offsets, not the offset into any box within the file (e.g. Media Data Box). This permits
referring to media data in files without any box structure. It does also mean that care must be taken
when constructing a self‐contained ISO file with its metadata

8.7.5.2
Syntax
aligned(8) class ChunkOffsetBox
extends FullBox(‘stco’, version = 0, 0) {
    unsigned int(32) entry_count;
    for (i=1; i <= entry_count; i++) {
        unsigned int(32) chunk_offset;
    }
}
aligned(8) class ChunkLargeOffsetBox
extends FullBox(‘co64’, version = 0, 0) {
    unsigned int(32) entry_count;
    for (i=1; i <= entry_count; i++) {
        unsigned int(64) chunk_offset;
    }

0000ff_60s_1280x720

000138f0: 00 25 00 00 00 25 00 00 00 2d 00 00 00 14 73 74 .%…%…-….st
00013900: 63 6f 00 00 00 00 00 00 00 01 00 00 00 30 00 00 co………..0..
00013910: 00 62 75 64 74 61 00 00 00 5a 6d 65 74 61 00 00 .budta…Zmeta..

00 00 00 30 // unsigned int(64) chunk_offset;

00000010: 69 73 6f 6d 69 73 6f 32 61 76 63 31 6d 70 34 31 isomiso2avc1mp41
00000020: 00 00 00 08 66 72 65 65 00 00 ef d8 6d 64 61 74 ….free….mdat
00000030: 00 00 02 af 06 05 ff ff ab dc 45 e9 bd e6 d9 48 ……….E….H
00000040: b7 96 2c d8 20 d9 23 ee ef 78 32 36 34 20 2d 20 ..,. .#..x264 –
00000050: 63 6f 72 65 20 31 35 37 20 72 32 39 36 39 20 64 core 157 r2969 d
00000060: 34 30 39 39 64 64 20 2d 20 48 2e 32 36 34 2f 4d 4099dd – H.264/M
00000070: 50 45 47 2d 34 20 41 56 43 20 63 6f 64 65 63 20 PEG-4 AVC codec
00000080: 2d 20 43 6f 70 79 6c 65 66 74 20 32 30 30 33 2d – Copyleft 2003-
00000090: 32 30 31 39 20 2d 20 68 74 74 70 3a 2f 2f 77 77 2019 – http://ww
000000a0: 77 2e 76 69 64 65 6f 6c 61 6e 2e 6f 72 67 2f 78 w.videolan.org/x

从30 地址开始是chunk的开始,及ES数据开始。

mp4 文件 stsc 解析

参考文档 14496-12

8.7.4.1
Sample To Chunk Box
Definition
Box Type: ‘stsc’
Container: Sample Table Box (‘stbl’)
Mandatory: Yes
Quantity: Exactly one
Samples within the media data are grouped into chunks. Chunks can be of different sizes, and the
samples within a chunk can have different sizes. This table can be used to find the chunk that contains a
sample, its position, and the associated sample description.
The table is compactly coded. Each entry gives the index of the first chunk of a run of chunks with the
same characteristics. By subtracting one entry here from the previous one, you can compute how many
chunks are in this run. You can convert this to a sample count by multiplying by the appropriate
samples‐per‐chunk.

aligned(8) class SampleToChunkBox
extends FullBox(‘stsc’, version = 0, 0) {
    unsigned int(32) entry_count;
    for (i=1; i <= entry_count; i++) {
        unsigned int(32) first_chunk;
        unsigned int(32) samples_per_chunk;
        unsigned int(32) sample_description_index;
    }
}

8.7.4.3
Semantics
version is an integer that specifies the version of this box
entry_count is an integer that gives the number of entries in the following table
first_chunk is an integer that gives the index of the first chunk in this run of chunks that share
the same samples‐per‐chunk and sample‐description‐index; the index of the first chunk in a
track has the value 1 (the first_chunk field in the first record of this box has the value 1,
identifying that the first sample maps to the first chunk).
samples_per_chunk is an integer that gives the number of samples in each of these chunks
sample_description_index is an integer that gives the index of the sample entry that
describes the samples in this ch

00012150: 02 00 00 00 00 01 00 00 04 00 00 00 00 1c 73 74 …………..st
00012160: 73 63 00 00 00 00 00 00 00 01 00 00 00 01 00 00 sc…………..
00012170: 05 dc 00 00 00 01 00 00 17 84 73 74 73 7a 00 00 ……….stsz..
00012180: 00 00 00 00 00 00 00 00 05 dc 00 00 03 90 00 00 …………….

00 00 00 01 //unsigned int(32) entry_count;

00 00 00 01 //unsigned int(32) first_chunk;

00 00 05 dc //unsigned int(32) samples_per_chunk;

00 00 00 01 //unsigned int(32) sample_description_index;

 

mp4 文件 stts 解析

8.6.1.2.1 Definition
Box Type: ‘stts’
Container: Sample Table Box (‘stbl’)
Mandatory: Yes
Quantity: Exactly one
This box contains a compact version of a table that allows indexing from decoding time to sample
number. Other tables give sample sizes and pointers, from the sample number. Each entry in the table
gives the number of consecutive samples with the same time delta, and the delta of those samples. By
adding the deltas a complete time‐to‐sample map may be built.
The Decoding Time to Sample Box contains decode time delta’s: DT(n+1) = DT(n) + STTS(n) where
STTS(n) is the (uncompressed) table entry for sample n.
The sample entries are ordered by decoding time stamps; therefore the deltas are all non‐negative.
The DT axis has a zero origin; DT(i) = SUM(for j=0 to i‐1 of delta(j)), and the sum of all deltas gives the
length of the media in the track (not mapped to the overall timescale, and not considering any edit list).
The Edit List Box provides the initial CT value if it is non‐empty (non‐zero).

http://wikil.lwwhome.cn:28080/wp-content/uploads/2020/04/0000ff_30s_11280x720.mp4

0000f250: 00 01 00 00 00 18 73 74 74 73 00 00 00 00 00 00 ……stts……
0000f260: 00 01 00 00 05 dc 00 00 02 00 00 00 00 28 73 74 ………….(st
0000f270: 73 73 00 00 00 00 00 00 00 06 00 00 00 01 00 00 ss…………..

aligned(8) class TimeToSampleBox
extends FullBox('stts', version = 0, 0) {
    unsigned int(32) entry_count;
    int i;
    for (i=0; i < entry_count; i++) {
        unsigned int(32) sample_count;
        unsigned int(32) sample_delta;
    }
}

00 00 00 01 //unsigned int(32) entry_count;

00 00 05 dc //unsigned int(32) sample_count;

00 00 02 00 //unsigned int(32) sample_delta;

mp4 文件 stss解析

sync sample atom确定media中的关键帧。对于压缩的媒体,关键帧是一系列压缩序列的开始帧,它的解压缩是不依赖于以前的帧。后续帧的解压缩依赖于这个关键帧。

sync sample atom可以非常紧凑的标记媒体内的随机存取点。它包含一个sample序号表,表内的每一项严格按照sample的序号排列,说明了媒体中的哪一个sample是关键帧。如果此表不存在,说明每一个sample都是一个关键帧,是一个随机存取点。

分析文件地址:

http://wikil.lwwhome.cn:28080/wp-content/uploads/2020/04/0000ff_30s_11280x720.mp4

8.6.2.1
Sync Sample Box
Definition
Box Type: ‘stss’
Container: Sample Table Box (‘stbl’)
Mandatory: No
Quantity: Zero or one
This box provides a compact marking of the sync samples within the stream. The table is arranged in
strictly increasing order of sample number.
If the sync sample box is not present, every sample is a sync sample.

参考文档 ISO/IEC 14496-12

4.2 Object Structure

aligned(8) class Box (unsigned int(32) boxtype,
        optional unsigned int(8)[16] extended_type) {
    unsigned int(32) size;
    unsigned int(32) type = boxtype;
    if (size==1) {
        unsigned int(64) largesize;
    } else if (size==0) {
        // box extends to end of file
    }
    if (boxtype=='uuid') {
        unsigned int(8)[16] usertype = extended_type;
    }
}
aligned(8) class FullBox(unsigned int(32) boxtype, unsigned int(8) v, bit(24) f)
    extends Box(boxtype) {
        unsigned int(8)
            version = v;
        bit(24)
            flags = f;
    }
aligned(8) class SyncSampleBox
extends FullBox(‘stss’, version = 0, 0) {
    unsigned int(32) entry_count;
    int i;
    for (i=0; i < entry_count; i++) {
        unsigned int(32) sample_number;
    }
}

0000f240: f8 00 00 00 00 10 70 61 73 70 00 00 00 01 00 00 ……pasp……
0000f250: 00 01 00 00 00 18 73 74 74 73 00 00 00 00 00 00 ……stts……
0000f260: 00 01 00 00 05 dc 00 00 02 00 00 00 00 28 73 74 ………….(st
0000f270: 73 73 00 00 00 00 00 00 00 06 00 00 00 01 00 00 ss…………..
0000f280: 00 fb 00 00 01 f5 00 00 02 ef 00 00 03 e9 00 00 …………….
0000f290: 04 e3 00 00 2e c8 63 74 74 73 00 00 00 00 00 00 ……ctts……

00 00 00 28 // box size

73 74 73 73 // box type stss

00 // box version 8bit

00 00 00 // box flags 24 bit

00 00 00 01 // SyncSampleBox unsigned int(32) entry_count;  = 1

00 00 00 01

00 00 00 fb

00 00 01 f5

00 00 02 ef

00 00 03 e9

00 00 04 e3

// unsigned int(32) sample_number;  总即6个关键帧,可以看出来视频60s视频每隔10s一个关键帧

 

ffmpeg 生成纯色的视频及添加音轨

ffmpeg -ss 0 -t 30  -f lavfi -i color=c=0x0000ff:s=1280x720:r=25 -vcodec libx264 -r:v 25 0000ff_30s_1280x720.mp4

同时添加一条音频track

ffmpeg -ss 0 -t 30  -f lavfi -i color=c=0x0000ff:s=1280x720:r=25  -ss 0 -t 30 -f lavfi -i anullsrc=channel_layout=stereo:sample_rate=48000  -vcodec libx264 -r:v 25 -c:a aac  0000ff_30s_1280x720_aac.mp4

同时添加两条不同格式音频track

ffmpeg -ss 0 -t 30  -f lavfi -i color=c=0x0000ff:s=1280x720:r=25  -ss 0 -t 30 -f lavfi -i anullsrc=channel_layout=5.1:sample_rate=48000 -ss 0 -t 30 -f lavfi -i anullsrc=channel_layout=stereo:sample_rate=48000  -map 0:v -map 1:a -map 2:a  -c:v libx264 -r:v 25 -c:a:0 ac3  -c:a:1 aac 0ff0ff_30s_1280x720_aac2_ac35.1.mp4

 

 

ffmpeg 倍速转码视频和音频

参考网页

https://trac.ffmpeg.org/wiki/How%20to%20speed%20up%20/%20slow%20down%20a%20video

ffmpeg -i  test.mp4 -filter_complex "[0:v]setpts=0.8*PTS[v];[0:a]atempo=1.25[a]" -map "[v]" -map "[a]" -b:v 400K -b:a 32k -c:v libx264 -s 1280x720  2_1280_720_264.mp4

上诉命令快放1.25倍,即将原来的视频时长缩短为80%.

Vim 用法入门文章地址

https://github.com/wsdjeg/vim-galore-zh_cn

命令 操作的行
:d 当前行
:.d 当前行
:1d 第一行
:$d 最后一行
:1,$d 所有行
:%d 所有行(这是 1,$ 的语法糖)
:.,5d 当前行至第 5 行
:,5d 同样是当前行至第 5 行
:,+3d 当前行及接下来的 3 行
:1,+3d 第一行至当前行再加 3 行
:,-3d 当前行及向上的 3 行(Vim 会弹出提示信息,因为这是一个保留的范围)
:3,'xdelete 第三行至标注 为 x 的那一行
:/^foo/,$delete 当前行以下,以字符 “foo” 开头的那一行至结尾
:/^foo/+1,$delete 当前行以下,以字符 “foo” 开头的那一行的下一行至结尾