VideoHelp Forum
+ Reply to Thread
Results 1 to 2 of 2
Thread
  1. Hello! Need help on how to merge video, audio, and multiple subtitles from one folder using ffmpeg with bash script. I'm using Google Colab to run the script with my Drive mounted on it.

    So, I have a folder on my Drive named "episode1" with the following files and want to merge them to .mkv.
    episode1_video.mp4
    episode1_audio.m4a
    episode1_subtitle_en.vtt
    episode1_subtitle_es.vtt
    episode1_subtitle_pt.vtt
    Merging them manually with this command:
    Code:
    ffmpeg -i "/content/drive/MyDrive/episode1/episode1_video.mp4" -i "/content/drive/MyDrive/episode1/episode1_audio.m4a" -i "/content/drive/MyDrive/episode1/episode1_subtitle_en.vtt" -i "/content/drive/MyDrive/episode1/episode1_subtitle_es.vtt" -i "/content/drive/MyDrive/episode1/episode1_subtitle_pt.vtt" \
    -map 0:v -map 0:a? -map 1 -map 2 -map 3 -map 4 \
    -c copy \
    -disposition:s:0 default \
    /content/drive/MyDrive/show_episode1.mkv \
    -hide_banner
    However, this is time-consuming since I need to copy-paste each file path manually, so I tried making a bash script. (Please note that I'm new to script-making and just mostly copied and combined what I found on the internet.)
    Code:
    #!/bin/bash
    
    input_directory="/content/drive/MyDrive/episode1"  #@param {type: "string"}
    output_path="/content/drive/MyDrive" #@param {type: "string"}
    file_name="show_episode1" #@param {type: "string"}
    
    for video_file in "$input_directory"/*.mp4; do
    base_path=${video_file%.*}
    base_name=${base_path##*/}
    
    for audio_file in "$input_directory"/*.m4a; do
    base_path=${audio_file%.*}
    base_name=${base_path##*/}
    
    for subtitle_file in "$input_directory"/*.vtt; do
    base_path=${subtitle_file%.*}
    base_name=${base_path##*/}
    
    ffmpeg -i "$video_file" -i "$audio_file" -i "$subtitle_file" -map 0:v -map 0:a? -map 1 -map 2 -map 3 -map 4 -c copy -disposition:s:0 default "$output_path"/"$file_name".mkv -hide_banner
    
    done
    done
    done
    But I get this error instead:
    Code:
    Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/content/drive/MyDrive/episode1/episode1_video.mp4':
      Metadata:
        major_brand     : isom
        minor_version   : 1
        compatible_brands: isomavc1mp42iso5iso6
      Duration: 00:01:09.47, start: 0.066733, bitrate: 3869 kb/s
        Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], 3862 kb/s, 29.97 fps, 29.97 tbr, 30k tbn, 59.94 tbc (default)
        Metadata:
          handler_name    : Bento4 Video Handler
    Input #1, mov,mp4,m4a,3gp,3g2,mj2, from '/content/drive/MyDrive/episode1/episode1_audio.m4a':
      Metadata:
        major_brand     : isom
        minor_version   : 1
        compatible_brands: isomiso5iso6
      Duration: 00:01:09.50, start: 0.000000, bitrate: 199 kb/s
        Stream #1:0(kor): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 192 kb/s (default)
        Metadata:
          handler_name    : Bento4 Sound Handler
    Input #2, webvtt, from '/content/drive/MyDrive/episode1/episode1_subtitle_en.vtt':
      Duration: N/A, bitrate: N/A
        Stream #2:0: Subtitle: webvtt
    Invalid input file index: 3.
    There's something wrong/missing with the script but can't figure out how to fix it. I want to merge everything including all the subtitles. Hope someone can help. Thank you!
    Last edited by oknchm; 13th Nov 2022 at 17:19.
    Quote Quote  
  2. I'm a Super Moderator johns0's Avatar
    Join Date
    Jun 2002
    Location
    canada
    Search Comp PM
    Unless you want to use ffmpeg all the time under a script why not use mkvmerge?
    I think,therefore i am a hamster.
    Quote Quote  



Similar Threads

Visit our sponsor! Try DVDFab and backup Blu-rays!