-2

I have created a script that takes the filename of screenshots from MPV player and grabs the time codes and cuts the video. I like MPV because it is very fast on big movie files and hitting s (screenshot) for every in and out cut is very easy. I have not found any bash script (I can only do bash or are learning bash) that can do this, only lua and java scripts.

The bash script:

#!/bin/bash

clear

DATE=$(date +"%Y%m%d_%H%M%S")

x-terminal-emulator  -geometry 50x20+3100+0 -e "bash -c 'while true; do clear;ls *.jpg;sleep 1;done' &"

rm *.jpg CUT*.mp4 cutLines.* cutMerge.*

mpv --screenshot-template="~/%F-(%P)-%03n" "$1"

echo
read -p "--- Hit ENTER to CUT ---"
echo

ls *.jpg | cut -c 24-35 > cutLines.txt

IFS=$'\n'
while IFS= read -r ONE; do read -r TWO
        echo " Making cut for duration: $ONE - $TWO stored as: CUT_${ONE}.mp4"
    ffmpeg -nostdin -loglevel quiet -ss "${ONE}" -to "${TWO}" -i "${1}" -c copy CUT_"${ONE}".mp4
        echo CUT_"${ONE}".mp4 >> cutMerge.tmp
done < cutLines.txt

cat cutMerge.tmp | sed "s/^/file '/" |sed "s/$/'/" > cutMerge.txt

ffmpeg -f concat -safe 0 -i cutMerge.txt -c copy CUTmerge_"$DATE".mp4

The script works for the clips.

Here is the link where you see what I struggle with. It looks like read line does not read all the data or something?

Video showing what the problem is

philipxy
  • 14,867
  • 6
  • 39
  • 83
Hamak
  • 1
  • 1
  • As the bash tag you used instructs - `For shell scripts with syntax or other errors, please check them at https://shellcheck.net before posting here.` – Ed Morton Aug 20 '22 at 12:25
  • Answers belong in answer posts, not question posts. I have rolled back you putting in an answer, please post an answer post. PS [Why should I not upload images of code/data/errors when asking a question?](https://meta.stackoverflow.com/q/285551/3404097) [Why are images of text, code and mathematical expressions discouraged?](https://meta.stackexchange.com/q/320052/266284) Put all & only what is needed to ask in your post, not (just) at a link. Especially don't expect us to watch a video. Link to things that are not necessary but may be of interest. Debug questions require a [mre]. [ask] [Help] – philipxy Aug 20 '22 at 13:30

1 Answers1

0

Thanks to Ed Morton's TIPS the script is now working! It looks like the problem was missing double quotes and the ffmpeg option -nostdin that was the main problem for this script.

#!/bin/bash

clear

rm *.jpg cutLines.txt cutMerge.txt cutMerge.tmp

DATE=$(date +"%Y%m%d_%H%M%S")

x-terminal-emulator  -geometry 50x20+3100+0 -e "bash -c 'while true; do clear;ls *.jpg;sleep 1;done' &"

mpv --screenshot-template="~/%F-(%P)-%03n" "$1"

echo
read -p "--- Hit ENTER to CUT ---"
echo

ls *.jpg | cut -c 24-35 > cutLines.txt

IFS=$'\n'
while IFS= read -r ONE; do read -r TWO
    echo " Making cut for duration: $ONE - $TWO stored as: CUT_${ONE}.mp4"
    ffmpeg -nostdin -loglevel quiet -ss "${ONE}" -to "${TWO}" -i "${1}" -c copy CUT_"${ONE}".mp4
    echo CUT_"${ONE}".mp4 >> cutMerge.tmp
done < cutLines.txt

cat cutMerge.tmp | sed "s/^/file '/" |sed "s/$/'/" > cutMerge.txt

ffmpeg -f concat -safe 0 -i cutMerge.txt -c copy VideoMerged_"$DATE".mp4


echo -e "\n--- cutLines"
cat cutLines.txt
echo -e "\n--- cutMerge\n"
cat cutMerge.txt

rm *.jpg cutLines.txt cutMerge.txt cutMerge.tmp

mpv VideoMerged_"$DATE".mp4

Hamak
  • 1
  • 1