Script to preserve subtitles in edited videos

jhyler

Member
**Version 1.1.2 August 27, 2022**
Version 1.1.2 replaces 1.1.0 and adds a couple of new changes to guard against problematic situations:
  • An explicit error If the video file isn't where the project thinks it is, but the project opened successfully anyway (normally it won't in this case).
  • An explicit error If the output video would overwrite the input video. (The script needs both open simultaneously, so overwriting poses a problem).
  • Better handling of the -f option and an explicit check for ffprobe and ffmpeg being on the path if -f is not used
**Version 1.1 August 26, 2022**
Version 1.1 is attached. Version 1.0 remains attached in case 1.1 doesn't work for you but 1.0 did. Improvements include:
  • The English-language check has been removed, along with the restriction that only a single subtitle stream would be corrected. The subtitler will now process all the subtitle streams in the input video, regardless of language.
  • "Subrip" subtitles are now supported, in addition to mov_text and ass.
  • Corrected a bug that allowed the last subtitle in a stream, under certain conditions, to be erroneously dropped.
  • Corrected a failure that would occur when a video contained subtitles, but none were left after all cuts were made.
  • Added special handling for English subtitles tagged incorrectly as "en" rather than "eng".
  • Added a -v option to print a version identifier, in case there should be any more updates.
  • Added a %* to the bat file so you can put additional options on the command line.
See "getting help" in case you should encounter any issues.

** Original Version **
This is something I wrote to preserve timed text (srt) or Advanced Substation Alpha (ass) subtitles in videos being edited with VideoRedo. It is used as follows:
  1. Install ffmpeg, if you don't already have it. I would suggest adding the path to the binaries into your path, but it isn't required.
  2. Modify the attached bat file with your choice of directories and options.
  3. Use VideoReDo to make cuts in your videos. I recommend you don't do anything other than make cuts. DO NOT SAVE YOUR VIDEOS - save project files instead, put them in the directory you specified in the bat file.
  4. Open a command window and type the command "subtitler" to start the bat file. The subtitler will open each of your projects in turn, save the video using the profile you selected, retrieve the first English subtitles it finds on the original video, modify their timing in accordance with your cuts, and (depending on the options you choose) either embed the updated subtitles into the output video or leave them in a .srt file with the video.
I strongly recommend the profile you choose saves your videos as mkv files. Almost all the use I've made of this script has been in creating mkv files, a little bit as mp4 files, and I've never tried anything else at all.

That's all there is to it. Please note that the default behavior of the program is to delete your project files after it uses them. You can disable this behavior with the -kp (keep projects) option.

Best of luck! I can't promise any support, but you can reply here with any feedback.
 

Attachments

Last edited:

reneg

New member
Brilliant. I've been looking for something that could handle timed text and edits with VideoRedo. Your script works well for me on the files that I've tried. Thank you for posting it.
 

bhiga

New member
This is great!! Thank you for sharing your work, also nice to see a fellow VBScripter. :cool:

MKV output works well, the only issue I ran into was an error deleting the temporary .ass file if it doesn't exist because there are no subtitles in the output scene - got around it with if fso.FileExists check.

My primary use is to split combined episode files, so I also customized it to use the project name as the output file name for the video and subtitle file - that way I can just save an appropriately-named project file for each episode since I already have the episode names and numbers added from FileBot. Using your script is much faster and less error-prone compared to my previous workflow of saving split scenes from VRD and renaming the output files afterward!

BTW, if you aren't already familiar with the site, SS64.com has been my go-to site for both batch/CMD and VBScript reference/examples (there's more there too) for decades and it's where I learned a key errorlevel evaluation difference between .bat and .cmd processing.
 

jumbo mumba

New member
Thank you for the script. I have ffmpeg in my PATH, I use the default directories, the project opens without issue in Videoredo. When I run the batch file, (located in C:\Scripts), it returns "unable to open project file: C:\Projects\webb.Vprj", Subs are srt format, Videoredo processes the project fine, just no subs. Ran as Administrator, made no difference. What am I missing? Any reply appreciated......
 

bhiga

New member
@jumbo mumba - I've seen that when the source asset referenced in the .Vprj can't be opened. Open C:\Projects\webb.Vprj (it should ask if it can't locate a file) in VideoReDo then resave and/or manually check the filepaths in the .Vprj in a text editor - it's XML.

It's helpful to enable debugging in the subtitler.bat by including the -d option so it pops up the overall and project output in IE windows. Otherwise just drop a WScript.Echo(dbgText) in the Debug sub if you just want it spit out to console which is my personal preference because I like scrolly madness.
 

jhyler

Member
Yes, the literal meaning of that message is that the script asked VideoRedo to open your project file, and it said it couldn't. The script then bails on processing that project and goes on to the next one (if there is one). It does not delete your project in this case, even if you have project deletion enabled. Bhiga is correct, first step is to eliminate the script entirely and try opening that project directly with VRD. Most likely it won't open, and you can continue debugging it from there. Could be something in the .Vprj itself, or maybe the video file isn't where the project thinks it is any more. If you can't figure it out, you may have to delete the project file and re-edit the video, creating a new project.

I have a set of fixes and a couple of enhancements to the script which I'll try to find time to upload. (This including a fix for bhiga's problem of a failure when the input video contains subtitles but none remain after making the cuts. That scenario just never occurred to me.) But I am very pessimistic that they might help with your particular problem.
 
Last edited:

jumbo mumba

New member
Thank you for the quick replies. As I stated in my post VRD has no trouble opening the project. Checked Vprj, file path is correct. Debug switch just returns same "unable to open project file". I have uploaded a small sample of mkv with embedded subs(srt), if someone wants to take a crack at it. "h**ps://drive.google.com/file/d/1wKmPM-eZeuU2ykA2O0QXx5D-MBuCrfAD/view?usp=sharing"
 

jhyler

Member
jumbo mumba - That's a variable frame rate video. VideoReDo doesn't work with VFR, though there have been rumblings that support might be coming. Until/unless it does, what you have to do is convert your videos to constant frame rate before you let VideoReDo have them. There are several tools that can do that for you, one of the better ones (IMO) is Handbrake. Handbrake has a bit of a learning curve if you're not familiar with it. Looks like all you should have to do is create a profile that passes through AAC audio and subtitles, and changes the video to (say) H.264 at 30 fps CFR. You may want to play with the video quality settings. Or not.

Interesting that VRD doesn't open the project in this case, since in interactive mode it does open VFR videos but just doesn't work right. I like the immediate rejection better, myself.
 
Last edited:

jumbo mumba

New member
As I stated twice, VRD has no trouble opening, editing, and saving the project and video in matroska format. It is not VFR. The discrepancy of 30 vs 29.97 seems to arise between ffmpeg(capture as ts) and mkvtoolnix(container remux). Re-multiplexing with ffmpeg does not show this issue. Regardless, VRD opens the mkv project without complaint.
 

jhyler

Member
jumbo momba - Sorry I'm unable to help you. I'll recap what I've done and what I can't do. First, the video you uploaded is, beyond question, VFR:
mi2.JPG

Or if you prefer ffmpeg output, the vfrdet filter shows:
[Parsed_vfrdet_0 @ 00000149937a03c0] VFR:0.733037 (659/240) min: 33 max: 34 avg: 33

So it is VFR, though the variance in frame duration is certainly small and may not raise issues when processed through VideoReDo. When that video is opened/edited/saved through the interactive VideoReDo interface, which no one disputes you can do (it's just the video processing results that can go awry), I get a warning on open:

Capture.JPG

Clicking yes and carrying on, I created a project to cut off the last second of the video. The subtitler opened the project and ran correctly, but didn't find the subtitles because for some reason they didn't have a language tag applied to them. I've not seen that happen before; perhaps I'll add an option that allows subtitles without language identified. But I failed to recreate your issue of the project file not opening through the VideoReDo API.

I don't know what else I can tell you. Since what you uploaded is just a snippet, perhaps whatever is causing your issue to occur got excluded. I would like to figure it out. Your workflow is different from mine, since I personally avoid HEVC and don't typically use mkvtoolnix, so it's possible that whatever is happening is related to one of those. But at the moment I don't know what it would be.
 

Attachments

Last edited:

jumbo mumba

New member
Thank you for your attempts to help me out. Haven't replied since I've been away. It is interesting that you report VFR, since Mediainfo on my end reports for the original transport capture 29.97.

transport.png

And for the original matroska remux constant. (Although 30 vs 29.97)

matroska.png

Since videoredo has no trouble opening the project, I have no idea what is going on.
 

NeoMaxx

New member
@jhyler

First of all thank you for your project (very good idea), however I can't get it to work properly and the subtitles are not taken into account I get the following error:
Bash:
C:\Scripts>subtitler
Processing C:\Projects\name-file-in-mkv.Vprj
There are no English subtitles in C:\Projects\name-file-in-mkv.mkv.
VRD File Save: 99%
I tried various methods to recognize the subtitle file, in particular to declare it in English, but nothing worked! Could you enlighten me for example so that it supports any subtitles regardless of the language?

Good for you.
 

jhyler

Member
NeoMaxx -

It could be a couple of things. The uploaded version only selects English subtitles. (At the time I was more worried about getting the script to work at all than foreign languages). If you run mediainfo on your video, you should see "Language : English" in the subtitle section of the video. The script does an ffprobe and looks for the "(eng)" that is supposed to be on English-language subtitle streams to determine that it's English. I have seen a very few videos, typically old ones, that have the old "(en)" code instead, and my script will miss them. There's also varieties that shouldn't be used but might be, like en-AU for Austrailian English, If any of that's what's causing your issue, it's possible to use ffmpeg to change the language code to "eng", but it's arcane and I have an alternative for you below.

The other thing it could be is the subtitle type isn't accepted by the script. The uploaded version of the script only accepts mov_text and ass subtitles. (The ffprobe output would contain something like "Stream #0:02(Eng): Subtitle: mov_text" (or ass). Anything else would be ignored. I have since added "subrip" as an acceptable type, but that change hasn't been uploaded yet. There are other subtitle types, though, that aren't supported. Some because I've never seen them, and I might be able to add them depending on how ffmpeg treats them. Other types are never going to get supported - these are the image-based subtit;les you normally find on dvds and blu-rays. If you do an ffprobe and see "DVDSUB" subtitles, or "VOBSUB", or "PGSSUB", to name a few, then I'm sorry but you're out of luck as far as my script goes. OTOH, I believe VRD has internal support for at least some of those types.

I have (hopefully) good news for you, though. There is a new version of my script that doesn't do the English-language check, it uploads multiple subtitle streams regardless of language. I was going to post it a while ago, but the site was down and I forgot about it. Now that you've shown me it's up again, I'll try to get it uploaded in the next day or two. What I suggest you do is retry with the new script, once it's uploaded. If your video still doesn't work then let me know, and it would then help if you could upload ffprobe and/or mediainfo output. Both if possible.
 
Last edited:

NeoMaxx

New member
@jhyler

Thank you for your answer and well seen for the after-sales service lol

I will wait quietly and try with the updated version.

For information, my source file is a .mkv which contains 3 tracks (1. video / 2. audio / 3. subtitles). The subtitle file is in .ass format and the language tag is of course English (I use MKVToolNix GUI for muxing)

See you soon 👍
 

jhyler

Member
A new version 1,1 has been posted. Details are in the top message. Testing of videos with multiple subtitle streams was more limited than I would have preferred, since the ones I have all came from a single source, but seems stable. Again, I can't promise support, but let me know how it goes and I'll do what I can. (See the "Getting help" file included in the distribution).
 

NeoMaxx

New member
@jhyler

Great, thank for your feedback and the software update, the "Get Help" file is also welcome 😉
I will do some tests during the day and let you know.
 

NeoMaxx

New member
After a few tests I get a new error that I don't know how to understand!?
Bash:
C:\Scripts>subtitler
Processing C:\Projects\my-video-project.Vprj
C:\Scripts\subtitler.vbs(539, 3) WshShell.Exec: Le fichier spécifié est introuvable.
Does the above error mean anything to you?

Code:
Général
Format                                   : Matroska
Version du format                        : Version 4
Taille du fichier                        : 2,11 Gio
Durée                                    : 1 h 42 min
Débit global moyen                       : 2 960 kb/s
Application utilisée                     : mkvmerge v63.0.0 ('Everything') 64-bit
Bibliothèque utilisée                    : libebml v1.4.2 + libmatroska v1.6.4

Vidéo
ID                                       : 1
Format                                   : AVC
Format/Info                              : Advanced Video Codec
Profil du format                         : High@L4
Paramètres du format                     : CABAC / 4 Ref Frames
Paramètres du format, CABAC              : Oui
Paramètres du format, RefFrames          : 4 images
Identifiant du codec                     : V_MPEG4/ISO/AVC
Durée                                    : 1 h 42 min
Débit                                    : 2 574 kb/s
Débit nominal                            : 3 000 kb/s
Largeur                                  : 1 920 pixels
Hauteur                                  : 800 pixels
Format à l'écran                         : 2,40:1
Type d'images/s                          : Constant
Images par seconde                       : 23,976 (24000/1001) Im/s
Espace de couleurs                       : YUV
Sous-échantillonnage de la chrominance   : 4:2:0
Profondeur des couleurs                  : 8 bits
Type de balayage                         : Progressif
Bits/(Pixel*Image)                       : 0.070
Taille du flux                           : 1,84 Gio (87%)
Bibliothèque utilisée                    : x264 core 163 r3060 5db6aa6
Paramètres d'encodage                    : cabac=1 / ref=3 / deblock=1:0:0 / analyse=0x3:0x113 / me=hex / subme=7 / psy=1 / psy_rd=1.00:0.00 / mixed_ref=1 / me_range=16 / chroma_me=1 / trellis=1 / 8x8dct=1 / cqm=0 / deadzone=21,11 / fast_pskip=1 / chroma_qp_offset=-2 / threads=24 / lookahead_threads=4 / sliced_threads=0 / nr=0 / decimate=1 / interlaced=0 / bluray_compat=0 / constrained_intra=0 / bframes=3 / b_pyramid=2 / b_adapt=1 / b_bias=0 / direct=1 / weightb=1 / open_gop=0 / weightp=2 / keyint=48 / keyint_min=25 / scenecut=0 / intra_refresh=0 / rc_lookahead=40 / rc=cbr / mbtree=1 / bitrate=3000 / ratetol=1.0 / qcomp=0.60 / qpmin=0 / qpmax=69 / qpstep=4 / vbv_maxrate=3000 / vbv_bufsize=3000 / nal_hrd=none / filler=0 / ip_ratio=1.40 / aq=1:1.00
Default                                  : Oui
Forced                                   : Non
Gamme de couleurs                        : Limited
Coordonnées de chromaticité              : BT.709
Caractéristiques du transfert            : BT.709
Coefficients de la matrice               : BT.709

Audio
ID                                       : 2
Format                                   : AAC LC
Format/Info                              : Advanced Audio Codec Low Complexity
Identifiant du codec                     : A_AAC-2
Durée                                    : 1 h 42 min
Débit                                    : 384 kb/s
Canaux                                   : 6 canaux
Channel layout                           : C L R Ls Rs LFE
Echantillonnage                          : 48,0 kHz
Images par seconde                       : 46,875 Im/s (1024 SPF)
Mode de compression                      : Avec perte
Taille du flux                           : 281 Mio (13%)
Titre                                    : VFF (AAC 5.1 @ 384 kb/s)
Langue                                   : Français
Default                                  : Oui
Forced                                   : Non

Texte
ID                                       : 3
Format                                   : UTF-8
Identifiant du codec                     : S_TEXT/UTF8
Identifiant du codec/Info                : UTF-8 Plain Text
Durée                                    : 26 min 11s
Débit                                    : 0 b/s
Count of elements                        : 9
Taille du flux                           : 196 Octets (0%)
Titre                                    : Forcé
Langue                                   : Français
Default                                  : Oui
Forced                                   : Oui
 

jhyler

Member
I'ts a runtime error, my high school French thinks it means 'file not found'. Could it be your video was deleted or moved so it's not where the project thinks it is? Or maybe you were saving the new file 'on top of' the original? Either would have caused that.

My home internet is down now, I'll look into it more as soon as possible and reupload. At least a more explicit message for not finding the video. Sorry, and thanks.
 
Last edited:

NeoMaxx

New member
@jhyler

After some research it came from ffprobe, I reinstalled ffmpeg and it works perfectly so once again a big thank you for your script and your help.
 
Top Bottom