Skip to content

Instantly share code, notes, and snippets.

@ftence
Last active November 5, 2019 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ftence/4e24bbb363e226bc4965c4798f63c08e to your computer and use it in GitHub Desktop.
Save ftence/4e24bbb363e226bc4965c4798f63c08e to your computer and use it in GitHub Desktop.
Quick and dirty scrip to upload a movie into a Warp 10™ instance
PATH=`realpath Bento4-SDK-1-5-1-628.x86_64-unknown-linux/bin`:$PATH
IN_FILE='what_is_warp_10.mp4'
OUT_DIR='tmp_movie_files'
WARP10_URL='http://127.0.0.1:8080/api/v0/update'
TOKEN='YOUR_WRITE_TOKEN'
SPLITS=50
rm -rf $OUT_DIR
mkdir -p $OUT_DIR
### FRAGMENT
mp4fragment --fragment-duration 1000 $IN_FILE $OUT_DIR/frag_$IN_FILE
cd $OUT_DIR
# Find the timescales for each track and timestamps for each fragment.
mapfile -t timescales < <( mp4info frag_$IN_FILE | grep timescale: | sed 's/[^0-9]//g' )
mapfile -t timestamps < <( mp4dump --verbosity 1 frag_$IN_FILE | grep "entry 0" | sed 's/.*time=\([0-9]*\).*/\1/g' )
### SPLIT
# split by segment
mp4split --media-segment segment.%llu.%04llu.m4s frag_$IN_FILE
# Rename to match time and split each split in $SPLITS splits.
ts_index=0
track_number=1
for timescale in "${timescales[@]}"; do
mapfile -t segment_files < <( ls segment.$track_number*.m4s )
for segment_file in "${segment_files[@]}"; do
ts_ms=$((${timestamps[ts_index]} * 1000000 / $timescale)) # Consider us time unit
split -n $SPLITS -d $segment_file $ts_ms.$segment_file.
rm -f $segment_file
((ts_index++))
done
((track_number++))
done
### CONVERT TO B64
# b64-encode each splits
for split_m4s in *.m4s.*; do
base64 -w 0 $split_m4s > $split_m4s.b64
done
# b64-encode init.mp4
base64 -w 0 init.mp4 > 0.segment.0.0000.mp4.00.b64
### GENERATE THE GTS INPUT FILE
current_ts_delta=0
for b64file in *.b64; do
# Get the
readarray -d . -t b64file_split <<< "$b64file"
ts=${b64file_split[0]}
track=${b64file_split[2]}
ts_split=$ts
if [ $track -gt 0 ]; then
ts_split=$(($ts + $current_ts_delta))
current_ts_delta=$((($current_ts_delta + 1) % $SPLITS))
fi
echo $ts_split// $IN_FILE{track=$track} b64:`cat $b64file` >> $IN_FILE.gts
done
### UPLOAD TO WARP 10
curl -H "X-Warp10-Token:$TOKEN" -T $IN_FILE.gts $WARP10_URL
### CLEAN
rm -rf $OUT_DIR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment