Skip to content

Instantly share code, notes, and snippets.

@cohnt
Last active August 16, 2026 19:18
Show Gist options
  • Select an option

  • Save cohnt/bc09fb8520e62e8361bd75f04871c665 to your computer and use it in GitHub Desktop.

Select an option

Save cohnt/bc09fb8520e62e8361bd75f04871c665 to your computer and use it in GitHub Desktop.
Steam Clip Conversion Script
#!/bin/bash
# Configuration
mkdir -p processed
mkdir -p to_delete
PROCESSED_ROOT=$(realpath processed)
TO_DELETE_ROOT=$(realpath to_delete)
# Iterate through each clip folder
for clip_dir in clips/*/; do
[ -e "$clip_dir" ] || continue
dir_name=$(basename "$clip_dir")
# Read all segment directories into an array to handle multiple folders
mapfile -t segment_dirs < <(find "$clip_dir" -name "init-stream0.m4s" -exec dirname {} \;)
if [[ ${#segment_dirs[@]} -eq 0 ]]; then
echo "No segments found in $dir_name, skipping."
continue
fi
# Track if all segments for this clip processed successfully
all_success=true
for segment_dir in "${segment_dirs[@]}"; do
segment_name=$(basename "$segment_dir")
# If there's only 1 segment, name it normally. If multiple, append the segment name.
if [[ ${#segment_dirs[@]} -eq 1 ]]; then
output_file="$PROCESSED_ROOT/${dir_name}.mp4"
else
output_file="$PROCESSED_ROOT/${dir_name}_${segment_name}.mp4"
fi
echo "Processing segment: $segment_dir"
pushd "$segment_dir" > /dev/null
# 1. Stitch Video Chunks
cat init-stream0.m4s chunk-stream0-*.m4s > temp_v.mp4
# 2. Stitch Audio Chunks (if they exist)
if [ -f "init-stream1.m4s" ]; then
cat init-stream1.m4s chunk-stream1-*.m4s > temp_a.m4a
ffmpeg -y -loglevel error -fflags +genpts -i temp_v.mp4 -i temp_a.m4a \
-c:v libx264 -preset fast -crf 23 -vf "scale=-2:720" -r 30 \
-c:a copy "$output_file"
rm -f temp_a.m4a
else
ffmpeg -y -loglevel error -fflags +genpts -i temp_v.mp4 \
-c:v libx264 -preset fast -crf 23 -vf "scale=-2:720" -r 30 "$output_file"
fi
status=$?
rm -f temp_v.mp4
popd > /dev/null
if [[ $status -ne 0 ]]; then
echo "Error: FFmpeg failed for $segment_dir"
all_success=false
fi
done
# Move source to 'to_delete' only if all segments processed successfully
if $all_success; then
echo "Success. Moving $dir_name to 'to_delete'."
mv "$(realpath "$clip_dir")" "$TO_DELETE_ROOT/"
else
echo "Error: One or more segments failed for $dir_name. Folder not moved."
fi
done
echo "Batch processing complete."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment