0

I have some -001.mkv files splitted in this way

the rings + of power-001.mkv
the rings + of power-002.mkv
..

nightmare.return-001.mkv
nightmare.return-002.mkv
..

I need to join -001.mkv files to obtain files like this

the rings + of power.mkv
nightmare.return.mkv

I thought of such a code, but doesn't work

for file in "./source/*001.mkv;" \
do \
    echo mkvmerge --join \
    --clusters-in-meta-seek -o "./joined/$(basename "$file")" "$file"; \
done

source is the source folder where -001.mkv files are located
joined is the target folder

Jack Rock
  • 65
  • 5
  • 1
    BTW, you're adding a lot of explicit line continuations that aren't actually necessary. I wonder if maybe you learned bash from examples in makefiles? One can need extra backslashes in a makefile to ensure that different lines are passed to the same shell interpreter, but that's not the case when you're writing a real/native bash script. – Charles Duffy Sep 02 '22 at 18:15
  • The immediate problem is that quoting the wildcard expression turns it into not a wildcard expression. See [When to wrap quotes around a shell variable?](https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable) – tripleee Sep 03 '22 at 05:41

1 Answers1

1

Can you try this?

for f in ./source/*-001.mkv
do
    p=${f%-001.mkv}
    echo mkvmerge --clusters-in-meta-seek \
        -o ./joined/"${p##*/}".mkv "$p"-*.mkv
done

Given your example, the for f in ./source/*-001.mkv should loop though nightmare.return-001.mkv and the rings + of power-001.mkv.
Let's assume that we're in the first iteration and $f expands to nightmare.return-001.mkv

  • ${f%-001.mkv} right-side-strips -001.mkv from $f; that would give ./source/nightmare.return. We store this prefix in the variable p.

  • ${p##*/} left-side-strips all characters up to the last / from $p; that would give nightmare.return.

So ... -o ./joined/"${p##*/}".mkv "$p"-*.mkv would be equivalent to ... -o ./joined/nightmare.return.mkv ./source/nightmare.return-*.mkv

PS: Once you checked that the displayed commands correspond to what you expect then you can remove the echo

Fravadona
  • 13,917
  • 1
  • 23
  • 35
  • 1
    Good answer. Might want to explain it? (Might also want to make it clear that the OP should take the `echo` off when they want to run it, and **not** pipe its output to a shell / copy-and-paste that output into a shell / etc; as the script is not currently going through the steps necessary to ensure `eval`-safe results). – Charles Duffy Sep 02 '22 at 18:13
  • @Fravadona it return me "**Error: The file '--join' could not be opened for reading: open file error.**" - please look how do I run your command [HERE](https://i.imgur.com/mOwefzk.gif) – Jack Rock Sep 02 '22 at 21:34
  • @JackRock I never used `mkvmerge` but according to the man, this option doesn't exist (and shouldn't be needed for your purpose) – Fravadona Sep 02 '22 at 23:30