Mrrrr's Forum (VIEW ONLY)
Un forum care ofera solutii pentru unele probleme legate in general de PC. Pe langa solutii, aici puteti gasi si alte lucruri interesante // A forum that offers solutions to some PC related issues. Besides these, here you can find more interesting stuff.
Lista Forumurilor Pe Tematici
Mrrrr's Forum (VIEW ONLY) | Reguli | Inregistrare | Login

POZE MRRRR'S FORUM (VIEW ONLY)

Nu sunteti logat.
Nou pe simpatie:
elenn
Femeie
24 ani
Bucuresti
cauta Barbat
24 - 55 ani
Mrrrr's Forum (VIEW ONLY) / Tutoriale si Ghiduri Utile // Tutorials and useful guides / [VIDEO] Extract Subtitles from MP4 and MKV Video Files with FFMPEG Moderat de TRaP, TonyTzu
Autor
Mesaj Pagini: 1
Mrrrr
AdMiN

Inregistrat: acum 17 ani
Postari: 2228
I copied ffmpeg.exe into the folder where I have my videos. Then ran cmd inside that folder by typing cmd into the folder path field instead of the folder path, and pressing ENTER.

First of all, assuming you don't use MediaInfo or some similar software which can be used to see track IDs within a video file.

You can use the following commad to see the available tracks within the video file (gonna use an mp4 file myself, but the command is the same):
ffmpeg -i input_file.mp4 or ffmpeg -i input_file.mkv

Note: Extracting subtitles from mkv files is risky, as sometimes they have 20ish subtitles and unless you check that the language/s you need are consistently on the same track in each file, you might extract a different language for some files.

This will display a bunch of information about codecs, metadata, and streams.
You should look for the first "Stream" word and check everyting after it. For example:

  Stream #0:0[0x1](und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(progressive), 1920x1080 [SAR 1:1 DAR 16:9], 3771 kb/s, 23.98 fps, 23.98 tbr, 24k tbn (default)
      Metadata:
        handler_name    : ?@GPAC1.1.0-DEV-rev5187-g1ffb067d9-HEAD
        vendor_id       : [0][0][0][0]
Stream #0:1[0x3](eng): Audio: eac3 (ec-3 / 0x332D6365), 48000 Hz, 5.1(side), fltp, 640 kb/s (default)
      Metadata:
        handler_name    : Audio Media Handler
        vendor_id       : [0][0][0][0]
      Side data:
        audio service type: main
Stream #0:2[0x4](spa): Subtitle: mov_text (tx3g / 0x67337874), 0 kb/s (default)
      Metadata:
        handler_name    : SubtitleHandler
  Stream #0:3[0x5](fra): Subtitle: mov_text (tx3g / 0x67337874), 0 kb/s
      Metadata:
        handler_name    : SubtitleHandler
  Stream #0:4[0x6](hun): Subtitle: mov_text (tx3g / 0x67337874), 0 kb/s
      Metadata:
        handler_name    : SubtitleHandler
  Stream #0:5[0x7](ron): Subtitle: mov_text (tx3g / 0x67337874), 0 kb/s
      Metadata:
        handler_name    : SubtitleHandler
  Stream #0:6[0x0]: Video: png, rgb24(pc, gbr/unknown/unknown), 350x197 [SAR 4724:4724 DAR 350:197], 90k tbr, 90k tbn (attached pic)
At least one output file must be specified


As you can see above, this mp4 file contains the usual first 2 streams which are video (stream id 0) and audio (stream id 1), then 4 subtitle streams identified as mov_text, then another entry described as "Video: png".
If you look through the MediaInfo utility, the last stream is not shown at all.
If you load the file in MKVToolnix, the last entry shows as "Global tags".

Whatever it is, we don't need it. We just need the 4 subtitles with IDs 2, 3, 4, and 5.

To extract them and name them accordingly (with language specified for each), we are going to create a batch file (.bat) in the folder with the videos. My first line of text in this tutorial was to copy ffmpeg.exe in the folder, for ease. Otherwise you would have to use full path to the ffmpeg file in the code below. But since ffmpeg is in my video folder, the code is just a bit simpler:

@echo off
for %%a in (*.mp4) do (
    echo Processing "%%a"...
    ffmpeg -i "%%a" ^
    -map 0:2 -c:s srt "%%~na_spa.srt" ^
    -map 0:3 -c:s srt "%%~na_fre.srt" ^
    -map 0:4 -c:s srt "%%~na_hun.srt" ^
    -map 0:5 -c:s srt "%%~na.srt"
)
pause


In a simple command line (as opposed to a .bat file) you would not need to double the %, but in a .bat file the doubling is required %%.

To explain the items in the code above:
for %%a in (*.mp4) do will loop through each input_file (%%a) with the .mp4 extension within the current folder
ffmpeg -i "%%a" will run the ffmpeg -i input_file command on the first file (after which the 2nd, 3rd etc. will follow)
• the ^ character is an escape character that acts as a line separator that tells the code to continue a command onto the next line without breaking it
-map 0:2 uses the track IDs identified in the results from running the simple "ffmpeg -i input_file.mp4" code the first time. 2 corresponds to the stream IDd as 2, which was the first subtitle, in Spanish
-c:s srt converts the mov_text subtitle to srt. If instead of mov_txt we would have subrip, this item could have been -c:s copy that would simply copy the srt file directly instead of converting it with -c:s srt
"%%~na_spa.srt" is where the output subtitle file is named with %%~na representing the same name as the video file without the .mp4 extension
pause keeps the console open after processing so you can review any errors or warnings

As stated before, the same works for .mkv files, where usually subtitles are stored as SRT and therefore instead of -c:s srt you could use -c:s copy for faster processing.

Source:
ChatGPT


_______________________________________


pus acum 2 saptamani
   
Mrrrr
AdMiN

Inregistrat: acum 17 ani
Postari: 2228
Extracting subtitles from mkv files is risky, as sometimes they have 20ish subtitles and unless you check that the language/s you need are consistently on the same track in each file, you might extract a different language for some files.

_______________________________________


pus acum 2 saptamani
   
Pagini: 1  

Mergi la