Thursday, April 5, 2012

mp4box subs: rejects blank empty srt line (ccextractor generated srt)

I use ripbot264 to transcode from mpg to mp4, saving 75 to 80% space, and embed cc/subtitles. Ripbot264 is gui (I believe it is delphi) frontend which automates a number of powerful freeware audio/video conversion tools. One of these tools is mp4box, which is used for the final mux.

The bug/error/prickliness I hit upon was that mp4box rejects (errors out on) srt subtitle files (extracted using ccextractor [todo link]) which contain multiline subtitles with empty line consisting of all spaces. (I think) such a line might be technically illegal, as empty lines act as record separators, but these empty lines consist of multiple spaces, so they should not be treated as empty lines. But I suspect mp4box trims leading/trailing spaces as preprocess, so it, incorrectly, looks just like the empty line delimiter.

The workaround is the following srtfix.bat which uses sed to detect multi-space empty line and replace the last space with '-'. It handles optional '^M' eol match with ".\?$" regexp. It has an extra bell & whistle, in that it first confirms that the srt is in fact invalid using mp4box own srt converter feature to see if it accepts the srt file.

@rem SRTFIX.bat (4/2012)
@rem fix blank lines in srt by adding - to the end

@rem first test srt

echo %~n1
@rem @goto END

c:\p\mp4box\mp4box -ttxt %1
@echo ERRORLEVEL=%ERRORLEVEL%
if %ERRORLEVEL%==0 goto NOERROR

@rem search blank space line, add '-' to end
sed "/^ *.\?$/s/ \(.\?\)$/-\1/" %1 > %~n1.fix
move %1 %1.orig
move %~n1.fix %1

goto END

:NOERROR
@rem cleanup
@del %~n1.ttxt

:END
some sample output and diff:
C:\Temp\RipBot264temp>dir s*
Volume in drive C is OS
Volume Serial Number is CA5D-3C54

Directory of C:\Temp\RipBot264temp

04/05/2012  12:06 PM           198,108 s.srt
04/04/2012  09:22 PM           198,108 s.srt.orig
04/05/2012  12:22 PM               417 srtfix.bat
           3 File(s)        396,633 bytes
           0 Dir(s)  97,269,633,024 bytes free

C:\Temp\RipBot264temp>fc s.srt s.srt.orig
Comparing files s.srt and S.SRT.ORIG
***** s.srt
WHEN I WAS 5.
                           -

***** S.SRT.ORIG
WHEN I WAS 5.


*****

***** s.srt
WHEN I WAS 5.
                           -
I SAW YOYO MA ON "MR. RODGERS
***** S.SRT.ORIG
WHEN I WAS 5.

I SAW YOYO MA ON "MR. RODGERS
*****

***** s.srt
00:33:24,036 --> 00:33:24,534
                           -
I SAW YOYO MA ON "MR. RODGERS
***** S.SRT.ORIG
00:33:24,036 --> 00:33:24,534

I SAW YOYO MA ON "MR. RODGERS
*****

No comments:

Post a Comment