#11 2017-02-01 20:00

lanceru
Member
Registered: 2015-09-29
Posts: 10

Re: Rename subtitle file based on ep number with video file names array

I wasn't sure if I should create another thread since this is a much broader question.

How do I assign an empty value to a String? I would later like to concatenate the string with 2 or possibly 3 characters, as seen with variable name 'EpisodeNumber'.

The following was denied:
s: String;
s = '';

var
  VideoFiles: TStringsArray;
  I, videoEpPOS, subEpPOS: Integer;
  currentVideoEpisode, currentSubtitleEpisode: String;
  BasePath, videoFileName, Extension: WideString;

Function GetVideoEpisodeNumber(const vFileName: String): String;
Var
  EpisodeNumber: String = '';
  {EpisodeNumber: String;}
  LDelim, RDelim : String;
  EpisodePos, RDelimPos, LDelimPos: integer;

begin
  LDelim := '][';  {If this isn't the 1st occurrence in the filename
                   consider utilizing 2 or 3  prior chars before episode number
                   instead of just 1}
  RDelim := ']';   {One char is usually enough}

  LDelimPos  := Pos(LDelim, vFileName);
  EpisodePos := LDelimPos + Length(LDelim);
  RDelimPos  := WidePosEx(RDelim, vFileName, EpisodePos);

  {EpisodeNumber = ''; } {Compile error as well}

  while EpisodePos < RDelimPos do begin
    EpisodeNumber += vFileName[EpisodePos];
    EpisodePos += 1;
    end;
  Result := EpisodePos;
end;

begin
  BasePath := WideExtractFilePath(FilePath);
  WideScanDirForFiles(BasePath, VideoFiles, False, False, False, '*.mp4');

  Extension := WideExtractFileExt(FileName);{(RightStr(FileName, 1, 4)); }

  subEpPOS := Pos('_01', 'Anime_Show_01') + 1;

  for I := 0 to Length(VideoFiles) - 1 do
  begin
    {currentVideoEpisode := copy(VideoFiles[i], videoEpPOS, 2); }
    currentSubtitleEpisode := copy(FileName, subEpPOS, 2);
    videoFileName := WideExtractBaseName(VideoFiles[i]);

    If (GetVideoEpisodeNumber(videoFileName) = currentSubtitleEpisode) Then
      FileName := videoFileName + Extension;
  end;
end.

Last edited by lanceru (2017-02-01 20:25)

Offline

#12 2017-02-01 21:03

Stefan
Moderator
From: Germany, EU
Registered: 2007-10-23
Posts: 1,161

Re: Rename subtitle file based on ep number with video file names array

>>I wasn't sure if I should create another thread ...

Me think, different question (same topic for you, but, nothing to do with the subject of this thread)
so better create (or search for) another thread.
That way we do not mix up question and answers (..."no, I meant the post above that post"...)
and also others are able to find answers better if there is a dedicated thread.
Maybe the "PascalScript Basics Basket by Stefan" thread would be an ideal place for such basic questions about PascalScript.




>>How do I assign an empty value to a String?
>>The following was denied:
>>s: String;
>>s = '';


Everywhere you did utilize the expression method by using the colon-equal operator (:=),
so why do you use now the "equal sign (=) operator" ?

Var
  EpisodeNumber: String;
  s: String;
begin
  EpisodeNumber := '';
  s := '';



HTH?
 


Read the  *WIKI* for HELP + MANUAL + Tips&Tricks.
If ReNamer had helped you, please *DONATE* to Denis or buy a PRO license. (Read *Lite vs Pro*)

Offline

#13 2017-02-01 22:41

lanceru
Member
Registered: 2015-09-29
Posts: 10

Re: Rename subtitle file based on ep number with video file names array

Thank you, I apologize for not spotting that.

In Lazarus, I could only initialize a variable at declaration with =, rather than with :=
Also, I noticed Renamer doesn't permit += assignment operator.

I guess the following would be considered the 'final/near final' revision, of this subtitle file renaming algorithm. Only thing I can think of is adding a user input/prompt box to ask for the extension and delimiters. I came across a thread on this forum with the tid=368 (can't post links); so hopefully that helps.


I'm very grateful for all assistance provided in this thread!

var
  VideoFiles: TStringsArray;
  I: Integer;
  currentVideoEpisode, currentSubtitleEpisode: String;
  BasePath, videoFileName, Extension: WideString;

Function GetEpisodeNumber(const vFileName, LDelim, RDelim: String): String;
Var
  EpisodeNumber: String;
  EpisodePos, RDelimPos, LDelimPos: integer;

  {Middle argument must be the first occurence in the filename, otherwise
  consider utilizing 2 or 3  prior chars before episode number
  instead of just 1. One char should be sufficient for the last argument.
  If episode numbers are the last char of the video name, then last argument
  should be '', i.e. two single quotation marks}
begin
  LDelimPos  := Pos(LDelim, vFileName);
  EpisodePos := LDelimPos + Length(LDelim);

  If RDelim = '' Then
    RDelimPos := Length(vFileName) + 1
  Else
    RDelimPos  := WidePosEx(RDelim, vFileName, EpisodePos);

  EpisodeNumber := '';

  while (EpisodePos < RDelimPos)  do begin
    EpisodeNumber :=  EpisodeNumber + vFileName[EpisodePos];
    EpisodePos :=  EpisodePos + 1;
    end;
  Result := EpisodeNumber;
end;

begin
  BasePath := WideExtractFilePath(FilePath);
  WideScanDirForFiles(BasePath, VideoFiles, False, False, False, '*.mp4');

  Extension := WideExtractFileExt(FileName);{(RightStr(FileName, 1, 4)); }

  for I := 0 to Length(VideoFiles) - 1 do
  begin
    videoFileName := WideExtractBaseName(VideoFiles[i]);
    currentVideoEpisode := GetEpisodeNumber(videoFileName, '][', ']')
    currentSubtitleEpisode := GetEpisodeNumber(FileName, 'w_', '.');

    If (currentVideoEpisode = currentSubtitleEpisode) Then
      FileName := videoFileName + Extension;
  end;
end.

Offline

#14 2017-02-02 11:31

Stefan
Moderator
From: Germany, EU
Registered: 2007-10-23
Posts: 1,161

Re: Rename subtitle file based on ep number with video file names array

Great you sorted that out on your own, well done.
Speaking variable names, code commented, us of Function (I still need to practice that part), fine.

- - -

>>>Renamer doesn't permit += assignment operator.
Yes, I think so too.
I use var := var + 'new content';

    output :=               'Default vars'           + #13#10;
    output := output + 'FilePath: '               + FilePath + #13#10;
    output := output + 'FileName: '              + FileName + #13#10 + #13#10;
    output := output + 'Extracted parts'       + #13#10;
    WideShowMessage(output);

- - -

>>>adding a user input/prompt box

You already found it, but here is a better link:
http://www.den4b.com/forum/viewtopic.php?id=368
http://www.den4b.com/forum/viewtopic.php?pid=9829#p9829

- - -

Unfortunately I still have not understood what was your renaming task (I am no native English speaker, maybe therefore)
Would you please be that kind an post 3-5 examples
This is what I had before:
..
..

This is what I have now:
..
..


So I can better understood what that script is for. Thank you.

- - -


 


Read the  *WIKI* for HELP + MANUAL + Tips&Tricks.
If ReNamer had helped you, please *DONATE* to Denis or buy a PRO license. (Read *Lite vs Pro*)

Offline

#15 2017-02-02 18:14

lanceru
Member
Registered: 2015-09-29
Posts: 10

Re: Rename subtitle file based on ep number with video file names array

Stefan wrote:

Unfortunately I still have not understood what was your renaming task (I am no native English speaker, maybe therefore)
Would you please be that kind an post 3-5 examples
This is what I had before:
..
..

This is what I have now:
..
..


So I can better understood what that script is for. Thank you.

- - -


 


In order for the script to work with this, I'll have to modify 3 lines of code. Video file extension, Left/Right Delimiters of the video file, Left/Right Delimiters of the subtitle files. The Right/Left Delimiters are used to locate the precise location of the episode number in the file names.

There are 10 episodes of a television show, one of them has the file name:
Ancient_Show.S01E01.480p.BluRay.x264.mkv

I downloaded the subtitle, separately, for the entire season. One of the subtitles has the following filename:
Ancient_Show - 1x01 - The Stolen Eagle.en.srt

I drag all the subtitle files into renamer. I then select the pascal script.

Alter the WideScanDir statement to match the video file extension, mp4 → mkv

WideScanDirForFiles(BasePath, VideoFiles, False, False, False, '*.mkv');

Here's the trick part. The left delimiter of the video and subtitle file, must be the first occurrence in the filename when reading from left to right.
This means that most of the time, at least 2 characters (and preferably 3) before the episode number must be specified. Also if you have more than one seasons of a television show in the same folder, you'll need to rename the files, one season at a time, otherwise you'll have duplicate errors.

Ancient_Show.S01E01.480p.BluRay.x264
Ancient_Show 1x01 - The Stolen Eagle.en.srt

    currentVideoEpisode := GetEpisodeNumber(videoFileName, '1E', '.')
    currentSubtitleEpisode := GetEpisodeNumber(FileName, 'x', ' ');  

Notice the video file name doesn't include the file extension due to WideExtractBaseName function. I know of WideExtractFilename, but I wanted to test my programming knowledge. If the episode number is the last part of the file name, then just use '', for the right delimiter.
Also, video and subtitle episode number must have same number of digits/left padding; which is usually 2 most of the time.

The left delimiter 1E, may be set to 01E as well.

I'll try to include another example in a few moments.

Another example
WideExtractBaseName yields the following videoname (without <>): <MSG 00 S1 Episode 01>  {notice extension is not included}
Subtile files have the following name when dragged into ReNamer: <Senshi.00.MSG.00.S01E24.DVDRip.QYQ.fr.srt>

I would assign the following delims to the following

    currentVideoEpisode := GetEpisodeNumber(videoFileName, 'de ', '') {be sure to include white space char after e}
    currentSubtitleEpisode := GetEpisodeNumber(FileName, '1E', '.');   

Full code in case I changed anything else.

begin
  LDelimPos  := Pos(LDelim, vFileName);
  EpisodePos := LDelimPos + Length(LDelim);

  If RDelim = '' Then
    RDelimPos := Length(vFileName) + 1
  Else
    RDelimPos := WidePosEx(RDelim, vFileName, EpisodePos);

  EpisodeNumber := '';

  while (EpisodePos < RDelimPos)  do begin
    EpisodeNumber :=  EpisodeNumber + vFileName[EpisodePos];
    EpisodePos :=  EpisodePos + 1;
    end;
  Result := EpisodeNumber;
end;

begin
  BasePath := WideExtractFilePath(FilePath);
  WideScanDirForFiles(BasePath, VideoFiles, False, False, False, '*.mkv');

  Extension := WideExtractFileExt(FileName);{(RightStr(FileName, 1, 4)); }

  for I := 0 to Length(VideoFiles) - 1 do
  begin
    videoFileName := WideExtractBaseName(VideoFiles[i]);
    currentVideoEpisode := GetEpisodeNumber(videoFileName, 'de ', '')
    currentSubtitleEpisode := GetEpisodeNumber(FileName, '1E', '.');

    If (currentVideoEpisode = currentSubtitleEpisode) Then
      FileName := videoFileName + Extension;
  end;
end.                          

Last edited by lanceru (2017-02-02 18:47)

Offline

Board footer

Powered by FluxBB