Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@dardo82
Last active September 16, 2022 01:53
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 dardo82/7f88dd375d7cea36dcc5e889ff2299e5 to your computer and use it in GitHub Desktop.
Save dardo82/7f88dd375d7cea36dcc5e889ff2299e5 to your computer and use it in GitHub Desktop.
Auto Video Edit
#!/bin/sh
SD="Foto/Persone/VIP/TeleVisione/RTV38/Carlotta_Comparini"
GDD="/Volumes/Misc Stuff/Google Drive/$USER/MultiMedia/Immagini"
ID=$(youtube-dl --get-id ytsearch:tadà_$(date -v-mon -v-0w +%d/%m))
VURL=$(curl -s "$(youtube-dl -g -f 135 youtu.be/$ID)" | gawk -v RS='<|>' '/"135"/{while($0!~/\/\//)getline; print}')
AURL=$(curl -s "$(youtube-dl -g -f 140 youtu.be/$ID)" | gawk -v RS='<|>' '/"140"/{while($0!~/\/\//)getline; print}')
cd $HOME/Pictures/$SD; youtube-dl --id --skip-download --write-auto-sub --sub-format=ttml --sub-lang=it youtu.be/$ID
eval $(gawk -F\" -v N=1 -v ID=$ID -v VURL=$VURL -v AURL=$AURL \
'/carlott/{PCNR=CNR; CNR=NR; CF4=$4; while($0!~/francesc/||NR-CNR<16)getline; \
if(CNR-PCNR>256){print "ffmpeg -i \""VURL"\" -i \""AURL"\" -ss "CF4" -to "$4" -c copy "ID"_"N"L.mp4;"; N=N+1}}' $ID.it.ttml)
for MP4 in ${ID}_[0-9]L.mp4; do FFMD=${MP4%.*}.ffmd; cp -v $MP4 "$GDD/$SD"
D=$(ffprobe -i $MP4 -show_format 2>&1 | gawk -F= '/duration/{print $2}')
ffmpeg -y -i $MP4 -af 'silencedetect=noise=-24dB:d=0.5' -f null - 2>&1 \
| gawk -v D=$D 'BEGIN{print";FFMETADATA1\n"; T=0} \
/end/{print "[CHAPTER]\nTIMEBASE=1/"10^6"\nSTART="T*10^6"\nEND="$5*10^6"\n"; T=$5} \
END{print "[CHAPTER]\nTIMEBASE=1/"10^6"\nSTART="T*10^6"\nEND="D*10^6"\n"}' > $FFMD
CMD="socat - /tmp/mpvsocket | grep -m1 chapter && socat - /tmp/mpvsocket <<< '{\"command\": [\"set_property\", \"pause\", true]}' && eval \$CMD"
mpv --fs --pause --keep-open --input-ipc-server=/tmp/mpvsocket --osd-level=2 --osd-fractions --chapters-file=$FFMD $MP4 & sleep 2 && eval "$CMD"
ffmpeg -f concat -safe 0 -i <(eval "printf \"file '$PWD/%s'\n\" ${MP4/./_[0-9].}") -c copy ${MP4/./S.}; cp -v ${MP4/./S.} "$GDD/$SD"
eval "rm -v ${MP4/./_[0-9].}"; rm -v $FFMD; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment