0

I am trying to integrate all my srt files into the mkv containers.

For that I wrote a script, which gets an mkv file (from another script) as $1, looks for srt files in the same directory (with the same basename as the videofile) and then builds an ffmpeg command.

The script (I know there is no security - for now I need to get it working...):

filename="$1"
medianame=${filename//'.mkv'/}
searchpattern=${medianame}\*
directory="$(dirname "${filename}")"
regex=".*\.(\w{2})(\.srt)$"

declare -A langcode
langcode[de]=ger
langcode[en]=eng
langcode[nl]=nld
langcode[ro]=ron

mapcount=0
args_subfiles=()
args_map=()
args_meta=()

while read line 
do
  if [[ $line =~ $regex ]]
  then
    mapcount=$((mapcount+1))
    args_map+=("-map ${mapcount}:s" )
    medianame=${filename//'.mkv'/}
    args_subfiles+=( ' -i '"$line" )
    lang=${BASH_REMATCH[1]}
    echo $lang
    args_meta+=('-metadata:s:s:'$mapcount' language='${langcode[$lang]} )
  fi

done <<< "$(find "$directory" -wholename "$searchpattern")"
echo "${args_subfiles[@]}"
echo "${args_map[@]}"
echo "${args_meta[@]}"

# subarg=$(echo "${args_subfiles[@]}")
ffmpeg -i "$filename" "${args_subfiles[@]}" -map 0 ${args_map[@]} -c copy ${args_meta[@]} "$out"

The ffmpeg output:

    ffmpeg version 5.0 Copyright (c) 2000-2022 the FFmpeg developers
  built with gcc 11 (GCC)
  configuration: --prefix=/root/ffmpeg_build --pkg-config-flags=--static --extra-cflags=-I/root/ffmpeg_build/include --extra-ldflags=-L/root/ffmpeg_build/lib --extra-libs=-lpthread --extra-libs=-lm --bindir=/root/bin --enable-gpl --enable-libfdk_aac --enable-libfreetype --enable-libmp3lame --enable-libopus --enable-libvpx --enable-libx264 --enable-cuda-nvcc --enable-cuvid --enable-nvenc --enable-libass --enable-opencl --cpu=native --enable-openssl --extra-libs='-lpthread -lm -lz' --enable-nonfree
  libavutil      57. 17.100 / 57. 17.100
  libavcodec     59. 18.100 / 59. 18.100
  libavformat    59. 16.100 / 59. 16.100
  libavdevice    59.  4.100 / 59.  4.100
  libavfilter     8. 24.100 /  8. 24.100
  libswscale      6.  4.100 /  6.  4.100
  libswresample   4.  3.100 /  4.  3.100
  libpostproc    56.  3.100 / 56.  3.100
Input #0, matroska,webm, from '/media/tv/La Brea/Season 1/La.Brea.S01E05.mkv':
  Metadata:
    encoder         : libebml v1.4.2 + libmatroska v1.6.4
    creation_time   : 2021-10-27T04:01:38.000000Z
  Duration: 00:43:02.59, start: 0.000000, bitrate: 7750 kb/s
  Stream #0:0(eng): Video: h264 (High), yuv420p(tv, bt709, progressive), 1920x1080 [SAR 1:1 DAR 16:9], 23.98 fps, 23.98 tbr, 1k tbn (default)
    Metadata:
      BPS             : 7107896
      DURATION        : 00:43:02.580000000
      NUMBER_OF_FRAMES: 61920
      NUMBER_OF_BYTES : 2294588959
      _STATISTICS_WRITING_APP: mkvmerge v62.0.0 ('Apollo') 64-bit
      _STATISTICS_WRITING_DATE_UTC: 2021-10-27 04:01:38
      _STATISTICS_TAGS: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES
  Stream #0:1(eng): Audio: eac3, 48000 Hz, 5.1(side), fltp, 640 kb/s (default)
    Metadata:
      BPS             : 640000
      DURATION        : 00:43:02.592000000
      NUMBER_OF_FRAMES: 80706
      NUMBER_OF_BYTES : 206607360
      _STATISTICS_WRITING_APP: mkvmerge v62.0.0 ('Apollo') 64-bit
      _STATISTICS_WRITING_DATE_UTC: 2021-10-27 04:01:38
      _STATISTICS_TAGS: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES
  Stream #0:2(eng): Subtitle: subrip
    Metadata:
      title           : English
      BPS             : 72
      DURATION        : 00:42:33.899000000
      NUMBER_OF_FRAMES: 800
      NUMBER_OF_BYTES : 23018
      _STATISTICS_WRITING_APP: mkvmerge v62.0.0 ('Apollo') 64-bit
      _STATISTICS_WRITING_DATE_UTC: 2021-10-27 04:01:38
      _STATISTICS_TAGS: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES
 -i /media/tv/La Brea/Season 1/La.Brea.S01E05.nl.srt: No such file or directory
2
  • set -x is your friend. Commented Feb 4, 2022 at 2:22
  • 1
    I don't have time to write an answer right now, but you should be adding one array element per argument. There are several places you're stuffing several arguments together into a single array element. set -x output will make this clear. Commented Feb 4, 2022 at 2:24

1 Answer 1

1

In the line:

args_subfiles+=( ' -i '"$line" )

it appends an array element which looks something like: ' -i file.srt'. Then the element is passed to ffmpeg as a single string -i file.srt, not the -i option followed by file.srt. Instead please modify the line as:

args_subfiles+=( '-i' "$line" )

then the commad line options are passed to ffmpeg properly. (Please note the whitespace in between. Two individual elements are appended to the array.)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.