1

So i have this script here Remove Audio Streams of videos - recursively without converting audio or video with FFMPEG But the subtitles part doesn't work. Currently I have tried a few variations of

export FLAGS='-sub_charenc CP1252 -i "${1%\.*}".srt -vcodec copy -acodec copy'

and

find "$TRANSCODEDIR"/* -type f -name "*.mkv" -exec bash -c 'ffmpeg -i "$1" "$FLAGS" "${1%\.*}".mkv ' _ {} \;

But I am getting errors like movie.mkv.srt does not exist or atleast one output file must be specified, and others, I am going to trial and error again and keep notes of what errors I get depending on what variation, but any help pointing me in the right direction is appreciated.

What code is supposed to do

File Tree Example

Movie.mkv Movie2.mkv Movie.srt Movie2.srt

It should add the correct srt to the correct mkv.

As it stands the error i get is that

Unrecognized option 'sub_charenc CP1252 -i "${1%\.*}".srt -vcodec copy -acodec c                                                                                                             opy'.
  • this is a hard to properly explain question and badly worded, but I will show my answer, as I have figured it out via trial and error... lots of trail and error – FreeSoftwareServers Aug 14 '15 at 5:02
  • I shouldn't say I have it figured out, as I think its impossible, but I came to the solution of making it a one liner.... see below comments – FreeSoftwareServers Aug 15 '15 at 8:11
1
  1. Create a script with the name export_flags

    #!/bin/bash
    srt_file_name="${1%\.*}".srt
    if [ -f "$srt_file_name" ]; then
            FLAGS="-i $srt_file_name -sub_charenc CP1252 -vcodec copy -acodec copy"
    else
            FLAGS=""
    fi
    export FLAGS="$FLAGS"
    
  2. Make it executable

    chmod +x export_flags
    
  3. Start a test with

    source export_flags your_mkv
    echo $FLAGS
    

    E.G.

    source export_flags SampleVideo_1080x720_1mb.mkv
    echo $FLAGS
    

    the output should be something like this

    -i SampleVideo_1080x720_1mb.srt -sub_charenc CP1252 -vcodec copy -acodec copy
    
  4. Use four find command

    find . -type f -name "*.mkv" -exec bash -c 'source export_flags "$1"; ffmpeg -i "$1" $FLAGS "${1%\.*}".mkv' _ {} \;
    

    In my case I have this

    ffmpeg version 2.5.8-0ubuntu0.15.04.1 Copyright (c) 2000-2015 the FFmpeg developers
      built with gcc 4.9.2 (Ubuntu 4.9.2-10ubuntu13)
      configuration: --prefix=/usr --extra-version=0ubuntu0.15.04.1 --build-suffix=-ffmpeg --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --shlibdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --enable-gpl --enable-shared --disable-stripping --enable-avresample --enable-avisynth --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmodplug --enable-libmp3lame --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-libschroedinger --enable-libshine --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libwavpack --enable-libwebp --enable-libxvid --enable-opengl --enable-x11grab --enable-libdc1394 --enable-libiec61883 --enable-libzvbi --enable-libzmq --enable-frei0r --enable-libvpx --enable-libx264 --enable-libsoxr --enable-gnutls --enable-openal --enable-libopencv --enable-librtmp --enable-libx265
      libavutil      54. 15.100 / 54. 15.100
      libavcodec     56. 13.100 / 56. 13.100
      libavformat    56. 15.102 / 56. 15.102
      libavdevice    56.  3.100 / 56.  3.100
      libavfilter     5.  2.103 /  5.  2.103
      libavresample   2.  1.  0 /  2.  1.  0
      libswscale      3.  1.101 /  3.  1.101
      libswresample   1.  1.100 /  1.  1.100
      libpostproc    53.  3.100 / 53.  3.100
    Input #0, matroska,webm, from './SampleVideo_1080x720_1mb.mkv':
      Metadata:
        ENCODER         : Lavf56.15.102
      Duration: 00:00:00.32, start: 0.080000, bitrate: 2642 kb/s
        Stream #0:0: Video: h264 (High), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], 25 fps, 25 tbr, 1k tbn, 50 tbc (default)
        Metadata:
          ENCODER         : Lavc56.13.100 libx264
        Stream #0:1: Audio: vorbis, 48000 Hz, 5.1, fltp (default)
        Metadata:
          ENCODER         : Lavc56.13.100 libvorbis
    Input #1, srt, from './SampleVideo_1080x720_1mb.srt':
      Duration: N/A, bitrate: N/A
        Stream #1:0: Subtitle: subrip
    File './SampleVideo_1080x720_1mb.mkv' already exists. Overwrite ? [y/N]
    
  • long story short I had trouble properly escaping the double quotes inside the variable FLAGS, the script works now, i just made one liners, but I would like to remove the ADD SUBTITLES lines, and make them into uncommentable FLAGS, just to keep it uniform, its hard to explain, so if you want, take a look at the linked script and it should make sense what I mean – FreeSoftwareServers Aug 14 '15 at 5:30
  • updated to what you said, no luck, its not translating the "${1%\.*}".srt inside the flag, If I remove the quotes around the export Flags=whatever instead of export FLAGS="whatever", then it says at least one output must be specified, IE: its not translating the "${1%\.*}".mkv – FreeSoftwareServers Aug 15 '15 at 8:01
  • I don't think what I want is possible, the same line for stripping audio, and for adding subtitles, as the subtitles line would require the exported variable FLAGS to have "${1%\.*}".srt expanded, but its exported before it can even do that, and if it could, I dont think it could re-expand it each time to the next item, the idea is recursive converting – FreeSoftwareServers Aug 15 '15 at 8:11
  • I will check this. – A.B. Aug 15 '15 at 8:31
  • Done, it should work now. – A.B. Aug 15 '15 at 13:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.