Author Topic: TV recordings renamed with XML metadata tag  (Read 2509 times)

MikeA

  • Newbie
  • *
  • Posts: 5
TV recordings renamed with XML metadata tag
« on: July 17, 2016, 07:43:43 AM »
Hi, I'm a real newbie. Registered just couple of minutes ago. I hope you can help me.

I have TV program metadata in XML format generated by MediaPortal:

Blacklist @  SE - 2016-05-19-1_25 - -TV5.xml
------------------
<?xml version="1.0" encoding="UTF-8"?>
<tags>
  <tag>
    <SimpleTag>
      <name>TITLE</name>
      <value>Blacklist (16)</value>
    </SimpleTag>
    <SimpleTag>
      <name>COMMENT</name>
      <value>Kausi 1, osa 19. Pavlovichin veljekset. Pahamaineiset Pavlovichin veljekset palaavat Washingtoniin, mutta Lizillä on muuta mietittävää. Hän on viimeinkin tajunnut, että Tom ei ole sitä, miltä näyttää. Kauhea totuus selviää Lizille Red</value>
    </SimpleTag>
    <SimpleTag>
      <name>GENRE</name>
      <value>movie/drama (general)</value>
    </SimpleTag>
    <SimpleTag>
      <name>CHANNEL_NAME</name>
      <value>TV5</value>
    </SimpleTag>
    <SimpleTag>
      <name>EPISODENAME</name>
      <value>
      </value>
    </SimpleTag>
    <SimpleTag>
      <name>SERIESNUM</name>
      <value>
      </value>
    </SimpleTag>
    <SimpleTag>
      <name>EPISODENUM</name>
      <value>
      </value>
    </SimpleTag>
    <SimpleTag>
      <name>EPISODEPART</name>
      <value>
      </value>
    </SimpleTag>
    <SimpleTag>
      <name>STARTTIME</name>
      <value>2016-05-19 01:21</value>
    </SimpleTag>
    <SimpleTag>
      <name>ENDTIME</name>
      <value>2016-05-19 02:20</value>
    </SimpleTag>
  </tag>
</tags>
------------------
The recording has the same filename with .ts extension:

Blacklist @  SE - 2016-05-19-1_25 - -TV5.ts

I want to rename both .ts and .xml files and
add 40 leftmost characters from the COMMENT tag in the middle of the filename.

Like this:

Blacklist @ Kausi 1, osa 19. Pavlovichin veljekset.  SE - 2016-05-19-1_25 - -TV5.xml
Blacklist @ Kausi 1, osa 19. Pavlovichin veljekset.  SE - 2016-05-19-1_25 - -TV5.ts

This is because here in Finland the COMMENT tag contains the series and episode
numbers of the TV shows and SERIESNUM, EPISODENUM and EPISODEPART are usually left empty.

Can I bulk rename filenames this way with ExifTool?

Regards,
Mike

Phil Harvey

  • ExifTool Author
  • Administrator
  • ExifTool Freak
  • *****
  • Posts: 16317
    • ExifTool Home Page
Re: TV recordings renamed with XML metadata tag
« Reply #1 on: July 17, 2016, 10:10:46 AM »
Hi Mike,

I can help more later when I have time, but right now all I can do is point you to the file renaming documentation and hope this helps a bit.  But it sounds like what you want to do is fairly complex, and will involve some advance formatting.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

MikeA

  • Newbie
  • *
  • Posts: 5
Re: TV recordings renamed with XML metadata tag
« Reply #2 on: July 17, 2016, 04:27:23 PM »
Thank you for your quick reply, Phil.

I decided to write the script with Visual Foxpro or Visual Basic. They both have powerful string handling functions. Anyway, thanks for your help and have a nice summer!

MikeA

  • Newbie
  • *
  • Posts: 5
Re: TV recordings renamed with XML metadata tag
« Reply #3 on: August 02, 2016, 09:29:04 PM »
This is off topic, but I made a short program with Visual Foxpro 9.0 and I thought that it may be useful for someone else who has the same problem. Here is the code:

----------------- snip ---------------------
* This program adds metadata to file names of MediaPortal TV recordings.
* This program is useful if series and episode info is inside the COMMENT tag.
* Preserves the original case of paths and file names.   
* Works with Visual Foxpro 9.0
*
CLEAR ALL
CLOSE ALL
CREATE TABLE Recordings (Recname c(250), Meta m) && Create a temp table
USE Recordings ALIAS Recordings && Open the table
DIMENSION Temparray(1,5) && Declare array to store file names
ofs=CREATEOBJECT('scripting.filesystemobject')
o=create('scripting.filesystemobject')
path = GETDIR()
IF EMPTY(path)
   WAIT 'Directory not selected' WINDOW
   RETURN
ENDIF
path = ofs.GetAbsolutePathName(path)
path = ADDBS(path)     
ln = ADIR(Temparray, path + '*.ts','A' ,1) && Use ADIR() function to get recording names into array
FOR i = 1 TO ln && Store file names into Recordings table
  lcFileName2=path + ofs.getfile(path + Temparray[i,1]).name
  IF .NOT. FILE(lcFileName2)
    WAIT 'Error locating a file' WINDOW
    RETURN
  ENDIF
  SELECT Recordings
  APPEND BLANK
  REPLACE Recname WITH Temparray(i,1)
  IF FILE(path + LEFT(TRIM(Recname), LEN(TRIM(Recname))-3) +'.xml') && Tries to find the xml file of the recording
    APPEND MEMO Meta FROM path + LEFT(TRIM(Recname), LEN(TRIM(Recname))-3) +'.xml'  && Metadata is added into the memo field
    Comment = SUBSTR(Meta,AT('<name>COMMENT</name>', Meta )+35)
    Comment = LEFT(Comment,AT('</value>', Comment)-1)
    Comment = CHRTRAN(Comment, '*/:<>?|\' + CHR(34), '_________' )  && Replacing invalid characters with underscore
    Addstr1 = LEFT(Comment,40)  && 40 characters from the beginning of COMMENT tag
    Addstr2 = RIGHT(Comment,40)  && 40 characters from the end of COMMENT tag
    IF LEN(Comment) < 81
      Addstr = '@ (' + Comment + ') '
    ELSE
      Addstr = '@ (' + Addstr1 + '...' + Addstr2 + ') '
    ENDIF
    Addstr = STRCONV(Addstr, 11)  && Converts UTF-8 characters to double-byte characters, works for Scandinavian character set
    lcNewFileName = STUFF(lcFileName2, AT('@',lcFileName2),1, Addstr)
    IF FILE(lcFileName2)
      o.movefile(lcFileName2, lcNewFileName) && Move/Rename .ts file
    ENDIF
    lcFileName2 = LEFT(TRIM(lcFileName2), LEN(TRIM(lcFileName2))-3) +'.xml'
    lcNewFileName = STUFF(lcFileName2, AT('@',lcFileName2),1, Addstr)
    IF FILE(lcFileName2)
      o.movefile(lcFileName2, lcNewFileName) && Move/Rename .xml file
    ENDIF
  ENDIF
NEXT i
RETURN
----------------- snip ---------------------

MikeA

  • Newbie
  • *
  • Posts: 5
Re: TV recordings renamed with XML metadata tag
« Reply #4 on: August 03, 2016, 09:55:17 PM »
Updated code:
----------------- snip ---------------------
* This program adds metadata to file names of MediaPortal TV recordings.
* This program is useful if series and episode info is inside the COMMENT tag.
* Preserves the original case of paths and file names.   
* Works with Visual Foxpro 9.0
* Looks like Win7 gives OLE error code 0x80070057 if files are on external drive bigger than 2TB,
* so this program version generates a file "bulkrename.bat" that you can run after this program ends
* Visual FoxPro does not recognize a path name properly if a disk or directory name contains an exclamation point (!).
* Running batch file bulkrename.bat gives error "the system cannot find file specified"
* but if you copy-paste individual command lines into Command Prompt windows, file renaming works just fine.
* Trying to fix this error here:
* http://www.sevenforums.com/general-discussion/399266-batch-renaming-files-fails-error-system-cannot-find-file.html

CLEAR ALL
CLOSE ALL
CREATE TABLE Recordings (Recname c(250), Meta m) && Create a temp table
USE Recordings ALIAS Recordings && Open the table
DIMENSION Temparray(1,5) && Declare array to store file names
ofs=CREATEOBJECT('scripting.filesystemobject')
o=CREATEOBJECT('scripting.filesystemobject')
path = GETDIR()
IF EMPTY(path)
   WAIT 'Directory not selected' WINDOW
   RETURN
ENDIF
path = ofs.GetAbsolutePathName(path)
path = ADDBS(path)
WAIT 'Press Y if you want to send commands to a batch file instead of executing them.' TO Retval WINDOW
IF ALLTRIM(UPPER(Retval))='Y'
  Writebatch=.T.
  IF FILE(path + 'bulkrename.bat')  && Does file exist?
    fhandle = FOPEN(path + 'bulkrename.bat',12)     && If so, open read/write unbuffered
  ELSE
    fhandle = FCREATE(path + 'bulkrename.bat')  && If not create it
  ENDIF
  IF fhandle < 0     && Check for error opening file
    WAIT 'Cannot open or create output file' WINDOW
    RETURN
  ENDIF
ELSE
  Writebatch=.F.
ENDIF
ln = ADIR(Temparray, path + '*.ts','A' ,1) && Use ADIR() function to get recording names into array
* DIMENSION lcFileName2(1,1) AS String, lcNewFileName(1,1) AS String
FOR i = 1 TO ln && Store file names into Recordings table
  lcFileName2=path + ofs.getfile(path + Temparray[i,1]).name
  IF .NOT. FILE(lcFileName2)
    WAIT 'Error locating a file' WINDOW
    RETURN
  ENDIF
  SELECT Recordings
  APPEND BLANK
  REPLACE Recname WITH Temparray(i,1)
  IF FILE(path + LEFT(TRIM(Recname), LEN(TRIM(Recname))-3) +'.xml') && Tries to find the xml file of the recording
    APPEND MEMO Meta FROM path + LEFT(TRIM(Recname), LEN(TRIM(Recname))-3) +'.xml'  && Metadata is added into the memo field
    Comment = SUBSTR(Meta,AT('<name>COMMENT</name>', Meta )+35)
    Comment = LEFT(Comment,AT('</value>', Comment)-1)
    Comment = CHRTRAN(Comment, '*/:<>?|\' + CHR(34), '_________' )  && Replacing invalid characters with underscore
    Addstr1 = LEFT(Comment,40)  && 40 characters from the beginning of COMMENT tag
    Addstr2 = RIGHT(Comment,40)  && 40 characters from the end of COMMENT tag
    IF LEN(Comment) < 81
      Addstr = '@ (' + Comment + ') '
    ELSE
      Addstr = '@ (' + Addstr1 + '...' + Addstr2 + ') '
    ENDIF
    Addstr = STRCONV(Addstr, 11)  && Converts UTF-8 characters to double-byte characters, works for Scandinavian character set
    lcNewFileName = STUFF(lcFileName2, AT('@',lcFileName2),1, Addstr)
    IF FILE(lcFileName2)
      IF Writebatch=.T.
        tmpstr = 'RENAME ' + CHR(34) + lcFileName2 + CHR(34) + ' ' + CHR(34) + JUSTFNAME(lcNewFileName) + CHR(34)
        FPUTS(fhandle, tmpstr)
      ELSE
        o.movefile(lcFileName2, lcNewFileName)
      ENDIF
    ENDIF
    lcFileName2 = LEFT(TRIM(lcFileName2), LEN(TRIM(lcFileName2))-3) +'.xml'
    lcNewFileName = STUFF(lcFileName2, AT('@',lcFileName2),1, Addstr)
    IF FILE(lcFileName2)
      IF Writebatch=.T.
        tmpstr = 'RENAME ' + CHR(34) + lcFileName2 + CHR(34) + ' ' + CHR(34) + JUSTFNAME(lcNewFileName) + CHR(34)
        FPUTS(fhandle, tmpstr)
      ELSE
        o.movefile(lcFileName2, lcNewFileName)
      ENDIF
    ENDIF
  ENDIF
NEXT i
IF Writebatch=.T.
  =FCLOSE(fhandle)     && Close file
  MODIFY FILE (path + 'bulkrename.bat') NOWAIT  && Open file in edit window
ENDIF
RETURN
----------------- snip ---------------------

MikeA

  • Newbie
  • *
  • Posts: 5
Re: TV recordings renamed with XML metadata tag
« Reply #5 on: August 04, 2016, 10:48:52 AM »
This is the final version. I'm going to post this also to the MediaPortal forum:
http://forum.team-mediaportal.com/

----------------- snip ---------------------
* FILE: bulkrename.prg
* This program adds metadata to file names of MediaPortal TV recordings.
* 40 leftmost and 40 rightmost characters of COMMENT tag in .xml file are added and
* the first occurrence of character @ in the file name marks the placing of the metadata.   
* This program is useful if series and episode info is inside the COMMENT tag.
* MediaPortal doesn't allow to use COMMENT tag in the filename format of TV recordings
* Windows 7 gives OLE error code 0x80070057 if files are on external drive bigger than 2TB,
* so this program gives the user option to directly rename all files in selected directory
* or write the rename commands into a batch file "bulkrename.cmd" that user can run later (recommended).
* Visual FoxPro does not recognize a path name properly if disk or directory name contains an exclamation point (!).
* Running batch file "bulkrename.cmd" may give an error "the system cannot find file specified",
* changing the character code page in the beginning of the batch file solves this problem.
* Default character set is ANSI (Windows-1252), that is character encoding of the Latin alphabet
* Preserves the original case of paths and file names.
* Works with Visual Foxpro 9.0.
* Example:
* Original filename: Blacklist @ SE_2016-05-19-1_25-TV5.ts
* Renamed file:      Blacklist @ (Series 1, Episode 19. The Pavlovich Brothers.) SE_2016-05-19-1_25-TV5.ts

CLEAR ALL
CLOSE ALL
CREATE TABLE Recordings (Recname c(250), Meta m) && Create a temp table
USE Recordings ALIAS Recordings && Open the table
DIMENSION Temparray(1,5) && Declare array to store file names
ofs=CREATEOBJECT('scripting.filesystemobject')
o=CREATEOBJECT('scripting.filesystemobject')
path = GETDIR()
IF EMPTY(path)
   WAIT 'Directory not selected' WINDOW
   RETURN
ENDIF
path = ofs.GetAbsolutePathName(path)
path = ADDBS(path)
WAIT 'Press Y if you want to send commands to a batch file instead of executing them.' TO Retval WINDOW
IF ALLTRIM(UPPER(Retval))='Y'
  Writebatch=.T.
  IF FILE(path + 'bulkrename.cmd')  && Does file exist?
    fhandle = FOPEN(path + 'bulkrename.cmd',12)     && If so, open read/write unbuffered
  ELSE
    fhandle = FCREATE(path + 'bulkrename.cmd')  && If not create it
  ENDIF
  IF fhandle < 0     && Check for error opening file
    WAIT 'Cannot open or create output file' WINDOW
    RETURN
  ELSE
    FPUTS(fhandle, 'chcp 1252')
  ENDIF
ELSE
  Writebatch=.F.
ENDIF
ln = ADIR(Temparray, path + '*.ts','A' ,1) && Use ADIR() function to get recording names into array
* DIMENSION lcFileName2(1,1) AS String, lcNewFileName(1,1) AS String
FOR i = 1 TO ln && Store file names into Recordings table
  lcFileName2=path + ofs.getfile(path + Temparray[i,1]).name
  IF .NOT. FILE(lcFileName2)
    WAIT 'Error locating a file' WINDOW
    RETURN
  ENDIF
  SELECT Recordings
  APPEND BLANK
  REPLACE Recname WITH Temparray(i,1)
  IF FILE(path + LEFT(TRIM(Recname), LEN(TRIM(Recname))-3) +'.xml') && Tries to find the xml file of the recording
    APPEND MEMO Meta FROM path + LEFT(TRIM(Recname), LEN(TRIM(Recname))-3) +'.xml'  && Metadata is added into the memo field
    Comment = SUBSTR(Meta,AT('<name>COMMENT</name>', Meta )+35)
    Comment = LEFT(Comment,AT('</value>', Comment)-1)
    Comment = CHRTRAN(Comment, '*/:<>?|\' + CHR(34), '_________' )  && Replacing invalid characters with underscore
    Addstr1 = LEFT(Comment,40)  && 40 characters from the beginning of COMMENT tag
    Addstr2 = RIGHT(Comment,40)  && 40 characters from the end of COMMENT tag
    IF EMPTY(Comment)
      Addstr = '@ ( ) '
    ELSE
      IF LEN(Comment) < 81
        Addstr = '@ (' + Comment + ') '
      ELSE
        Addstr = '@ (' + Addstr1 + '...' + Addstr2 + ') '
      ENDIF
    ENDIF
    Addstr = STRCONV(Addstr, 11)  && Converts UTF-8 characters to double-byte characters, works for Scandinavian character set
    lcNewFileName = STUFF(lcFileName2, AT('@',lcFileName2),1, Addstr)
    IF FILE(lcFileName2)
      IF Writebatch=.T.
        tmpstr = 'RENAME ' + CHR(34) + lcFileName2 + CHR(34) + ' ' + CHR(34) + JUSTFNAME(lcNewFileName) + CHR(34)
        FPUTS(fhandle, tmpstr)
      ELSE
        o.movefile(lcFileName2, lcNewFileName)
      ENDIF
    ENDIF
    lcFileName2 = LEFT(TRIM(lcFileName2), LEN(TRIM(lcFileName2))-3) +'.xml'
    lcNewFileName = STUFF(lcFileName2, AT('@',lcFileName2),1, Addstr)
    IF FILE(lcFileName2)
      IF Writebatch=.T.
        tmpstr = 'RENAME ' + CHR(34) + lcFileName2 + CHR(34) + ' ' + CHR(34) + JUSTFNAME(lcNewFileName) + CHR(34)
        FPUTS(fhandle, tmpstr)
      ELSE
        o.movefile(lcFileName2, lcNewFileName)
      ENDIF
    ENDIF
  ENDIF
NEXT i
IF Writebatch=.T.
  =FCLOSE(fhandle)     && Close file
  MODIFY FILE (path + 'bulkrename.cmd') NOWAIT  && Open file in edit window
ENDIF
RETURN
----------------- snip ---------------------