Skip to content

Instantly share code, notes, and snippets.

@antoniozh
Last active October 30, 2020 18:22
Show Gist options
  • Save antoniozh/76b120024629b47835ffbe83894c110e to your computer and use it in GitHub Desktop.
Save antoniozh/76b120024629b47835ffbe83894c110e to your computer and use it in GitHub Desktop.
Crops a portion of a youtube video and merges the video and audio parts without downloading the whole video.
#!/bin/bash
echo "Usage: ytcrop.sh VIDEO_LINK START_TIME DURATION [filename]"
LINKS=$(youtube-dl -g $1)
# Parse first link which is video
VIDEO_LINK=$(echo $LINKS | tr " " "\n" | head -n1 )
# Parse second link which is audio
AUDIO_LINK=$(echo $LINKS | tr " " "\n" | tail -n1 )
# echo Video link: $VIDEO_LINK
# echo Audio link: $AUDIO_LINK
if [ -n $4 ]; then
OUT_PATH="out.mp4"
else
OUT_PATH=$4
fi
ffmpeg -loglevel warning -hide_banner -ss $2 -i "${VIDEO_LINK}" -ss $2 -i "${AUDIO_LINK}" -map 0:v -map 1:a -t $3 -c copy $OUT_PATH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment