Skip to content

Instantly share code, notes, and snippets.

@kowalcj0
Last active July 30, 2023 10:09
Show Gist options
  • Save kowalcj0/ae0bdc43018e2718fb75290079b8839a to your computer and use it in GitHub Desktop.
Save kowalcj0/ae0bdc43018e2718fb75290079b8839a to your computer and use it in GitHub Desktop.
Extract all subtitles from a movie using ffprobe & ffmpeg
alias subs=subs
function subs() {
movie="${1}"
filename="${1%.*}"
mappings=`ffprobe -loglevel error -select_streams s -show_entries stream=index:stream_tags=language -of csv=p=0 "${movie}"`
OLDIFS=$IFS
IFS=,
( while read idx lang
do
echo "Exctracting ${lang} subtitle #${idx} from ${movie}"
ffmpeg -nostdin -hide_banner -loglevel quiet -i "${movie}" -map 0:"$idx" "${filename}_${lang}_${idx}.srt"
done <<< "${mappings}" )
IFS=$OLDIFS
}
@ripsnortntear
Copy link

ripsnortntear commented Jul 30, 2023

The issue im having is this producing 0kb sub files.

#!/usr/bin/env bash

function subs() {
  local movie idx lang subs=
  local -a dests=()
  for movie
  do
    ffprobe -loglevel error -select_streams s -show_entries stream=index:stream_tags=language -of csv=p=0 "$movie" |
    {
      while IFS=, read idx lang
      do
        subs+=" ${lang}_$idx"
        dests+=(-map "0:$idx" "${movie%.*}_${lang}_$idx.srt")
      done
      if test -n "$subs"
      then
        echo "Extracting subtitles from $movie:$subs"
        ffmpeg -nostdin -y -hide_banner -loglevel quiet -i "$movie" "${dests[@]}"
      else
        echo "No subtitles in $movie"
      fi
    }
  done
}

subs "${1}"

Screenshot 2023-07-30 060839

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment