-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chunked content script - see cta-wave/Test-Content#41
- Loading branch information
Showing
4 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#!/usr/bin/bash | ||
set -eux | ||
|
||
export BATCH="2023-04-28" | ||
|
||
export MPD=stream.mpd | ||
export INPUT_STREAM_ID=t16 | ||
export INPUT_DIR=output/cfhd_sets/12.5_25_50/$INPUT_STREAM_ID/$BATCH/ | ||
export OUTPUT_STREAM_ID=chunked | ||
export OUTPUT=chunked | ||
export OUTPUT_DIR=output/cfhd_sets/12.5_25_50/$OUTPUT_STREAM_ID/$BATCH/ | ||
|
||
|
||
# clean copy of input | ||
mkdir -p $OUTPUT_DIR | ||
rm -rf $OUTPUT_DIR/* | ||
cp -r $INPUT_DIR/* $OUTPUT_DIR/ | ||
|
||
|
||
####################################################### | ||
# chunking | ||
pushd $OUTPUT_DIR/1 | ||
|
||
## styp generation: take it from the first segment | ||
head -c12 0.m4s > styp | ||
|
||
## chunk segments | ||
for f in `ls *.m4s`; do | ||
$(dirname $0)/isobmff_chunker.py 5 `basename $f .m4s` | ||
rm $f | ||
done | ||
|
||
rm styp | ||
|
||
## modify MPD | ||
cp $(dirname $0)/stream.mpd ../stream.mpd | ||
|
||
popd | ||
####################################################### | ||
|
||
# zip | ||
pushd output | ||
rm cfhd_sets/12.5_25_50/$OUTPUT_STREAM_ID/$BATCH/$INPUT_STREAM_ID.zip | ||
zip -r cfhd_sets/12.5_25_50/$OUTPUT_STREAM_ID/$BATCH/$OUTPUT_STREAM_ID.zip cfhd_sets/12.5_25_50/$OUTPUT_STREAM_ID/$BATCH/* | ||
popd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
#!/usr/bin/env python3 | ||
import sys | ||
import struct | ||
|
||
def read_bytes(fobj, bytes): | ||
data = fobj.read(bytes) | ||
if len(data) != bytes: | ||
raise RuntimeError('Not enough data: requested %d, read %d' % (bytes, len(data))) | ||
return data | ||
|
||
def read_ulong(fobj): | ||
return struct.unpack('>L', read_bytes(fobj, 4))[0] | ||
|
||
def read_ulonglong(fobj): | ||
return struct.unpack('>Q', read_bytes(fobj, 8))[0] | ||
|
||
def read_fourcc(fobj): | ||
return read_bytes(fobj, 4) | ||
|
||
def read_atom(fobj): | ||
pos = fobj.tell() | ||
size = read_ulong(fobj) | ||
type = read_fourcc(fobj) | ||
|
||
if size == 1: | ||
size = read_ulonglong(fobj) | ||
elif size == 0: | ||
fobj.seek(0, 2) | ||
size = fobj.tell() - pos | ||
fobj.seek(pos + 8) | ||
|
||
return size, type | ||
|
||
def isobmff_read_topboxes(f): | ||
size = 0 | ||
f.seek(0) | ||
pos = 0 | ||
f.seek(0, 2) | ||
end = f.tell() | ||
f.seek(0) | ||
|
||
result = [] | ||
while 1: | ||
if pos + size < end: | ||
pos += size | ||
f.seek(pos) | ||
a = read_atom(f) | ||
result.append(a) | ||
size = a[0] | ||
else: | ||
break | ||
|
||
return result | ||
|
||
if __name__ == "__main__": | ||
if len(sys.argv) != 3: | ||
print(f"Usage: {sys.argv[0]} num_frag_per_chunk input_file") | ||
sys.exit(1) | ||
|
||
num_frag_per_chunk = int(sys.argv[1]) | ||
input_file = sys.argv[2] | ||
|
||
# open and parse | ||
fi = open(input_file + ".m4s", 'rb') | ||
boxes = isobmff_read_topboxes(fi) | ||
fi.seek(0) | ||
|
||
# iterate on boxes | ||
i_moof = -1 | ||
i_chunk = -1 | ||
global fo | ||
fo = None | ||
for b in boxes: | ||
bytes = fi.read(b[0]); | ||
if b[1] == b'styp': | ||
continue | ||
|
||
if b[1] == b'moof': | ||
i_moof += 1 | ||
|
||
if i_moof % num_frag_per_chunk == 0 and i_moof / num_frag_per_chunk != i_chunk: | ||
# new chunk | ||
if fo is not None: | ||
fo.close() | ||
i_chunk += 1 | ||
fo = open(input_file + "_" + str(i_chunk) + ".m4s", 'wb') | ||
styp = open("styp", 'rb') | ||
fo.write(styp.read(12)) | ||
styp.close(); | ||
|
||
fo.write(bytes) | ||
|
||
# close | ||
fo.close() | ||
fi.close() | ||
sys.exit(0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?xml version="1.0" ?> | ||
<!-- MPD file Generated with GPAC version 2.3-DEV-rev222-g4c2ad897f-ffenc_timescale_cbr at 2023-04-29T11:15:38.134Z --> | ||
<MPD xmlns="urn:mpeg:dash:schema:mpd:2011" minBufferTime="PT2.000S" type="static" mediaPresentationDuration="PT0H0M30.000S" maxSegmentDuration="PT0H0M2.000S" profiles="urn:mpeg:dash:profile:isoff-live:2011,urn:mpeg:dash:profile:cmaf:2019,urn:cta:wave:test-content-media-profile:2022"> | ||
<ProgramInformation> | ||
<Title>croatia, 1920x1080, 25.0fps, cfhd, Test Vector 16</Title> | ||
<Source>croatia_L1_1920x1080@25_30 version 4 (2023-02-13)</Source> | ||
<Copyright>© Croatia (2019), credited to EBU, used and licensed under Creative Commons Attribution 4.0 International (CC BY 4.0) (https://creativecommons.org/licenses/by/4.0/) by the Consumer Technology Association (CTA)® / annotated, encoded and compressed from original.</Copyright> | ||
</ProgramInformation> | ||
<Period duration="PT0H0M30.000S"> | ||
<AdaptationSet segmentAlignment="true" maxWidth="1920" maxHeight="1080" maxFrameRate="25" par="16:9" lang="und" startWithSAP="1" subsegmentAlignment="true" subsegmentStartsWithSAP="1" contentType="video" containerProfiles="cmf2 cfhd"> | ||
<SegmentTemplate media="1/$Time$_$SubNumber$.m4s" initialization="1/init.mp4" timescale="12800"> | ||
<SegmentTimeline> | ||
<S t="0" d="25600" k="2" r="14"/> | ||
</SegmentTimeline> | ||
</SegmentTemplate> | ||
<Representation id="1" mimeType="video/mp4" codecs="avc1.640028" width="1920" height="1080" frameRate="25" sar="1:1" bandwidth="4600000"> | ||
</Representation> | ||
</AdaptationSet> | ||
</Period> | ||
</MPD> |