Skip to content

Instantly share code, notes, and snippets.

@grahamwren
Last active April 23, 2023 16:58
Show Gist options
  • Save grahamwren/5df994e5972aaf22fe550f6649d23dc9 to your computer and use it in GitHub Desktop.
Save grahamwren/5df994e5972aaf22fe550f6649d23dc9 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
bucket=$1
mkdir -p /tmp/buckets
W_DIR=/tmp/buckets/$bucket
# pull down the bucket into the working dir
aws s3 sync s3://$bucket $W_DIR
# open the working dir
cd $W_DIR/hls/0
SED=$(if which gsed > /dev/null; then echo "gsed"; else echo "sed"; fi)
SHORT_ID_ALPHA="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_\\-"
# initialize an index
i=0
# traverse each stream_id
for stream_id in `ls stream-*-*.ts | $SED 's/^.*stream-\(['"$SHORT_ID_ALPHA"']\{9,11\}\)-[0-9]\+\.ts$/\1/g' | grep -v offline | uniq`; do
# concat all *.ts files per stream_id
(ls stream-$stream_id-*.ts | sort -V | xargs cat) > /tmp/all.ts
# convert concat'd ts file into mp4 in the base working dir
ffmpeg -y -i /tmp/all.ts -acodec copy -vcodec libx265 $W_DIR/stream-$(date +%Y-%m-%d)-$((++i)).mp4
# remove original segmented *.ts files
rm stream-$stream_id-*.ts
done
# upload files back to s3
aws s3 sync --delete --acl public-read $W_DIR s3://$bucket
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment