Advertisement
Guest User

Untitled

a guest
Jan 26th, 2016
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.04 KB | None | 0 0
  1. It does. I am currently having an issue with ffmpeg, but the script does work. I'll post the most recent version of the script:
  2. !/bin/bash
  3.  
  4. mythpostprocess.sh written by Justin Decker, copyright 2015. For licensing purposes, use GPLv2
  5.  
  6. #
  7. This script does four things:
  8.  
  9. - Flags and removes commercials from the recording.
  10.  
  11. - Transcodes video to h264 but retains the original audio (if you use an HDHomeRun
  12.  
  13. like I do, then that will probably be AC3 and doesn't need transcoding.)
  14.  
  15. - Adjusts the database with the new stream name/info.
  16.  
  17. - Creates a symlink with a pretty name to a different folder and prunes any broken
  18.  
  19. links and empty dirs as well as no longer needed video files (to keep up with MythTV's auto expiration system.)
  20.  
  21. #
  22. To use, create as a job that looks like this: /path/to/script/mythpostprocess.sh "%CHANID%" "%STARTTIMEUTC%"
  23.  
  24. The following values adjust the script parameters:
  25.  
  26. #
  27. Set this to where the pretty links should reside, making sure to include the trailing /.
  28.  
  29. PRETTYDIRNAME="/mediasrv/plex/tv/recorded/"
  30. Set this to the URL prefix of your Plex Media Server. Only needed if you want to notify Plex to refresh the library.
  31.  
  32. PMSURL="http://192.168.1.105:32400/"
  33. Set this to the section number of your recorded TV shows library. To find this out, go to your plex media server and navigate to the desired library. Look at the URL for that page, and at the end you should see /section/. The number here is your section number.
  34.  
  35. PMSSEC="2"
  36. Number of threads to use for encoding. 0 uses all.
  37.  
  38. THREADS=0
  39. Set the libx264 CRF value. Higher value is lower video quality but smaller file size, min 0 max 51. In my experience, 30 is reasonable. See ffmpeg manual.
  40.  
  41. CRF=22
  42. libx264 preset. See ffmpeg manual for other options. I set this to ultrafast because it runs under one among many of my hypervisors on this machine, and I'm aiming to code 2 seconds of video per real-time second. You may prefer normal if you aren't doing this and/or have a faster CPU.
  43.  
  44. PRESET="superfast"
  45. Set this to the location of the mythtv config.xml file. It's needed to determine the mysql login. If you're running mythbuntu, you shouldn't need to change this.
  46.  
  47. CONFIGXML="/home/mythtv/.mythtv/config.xml"
  48. Leave everything below this line alone unless you know what you're doing.
  49.  
  50. #
  51. Discover mysql username and password from mythtv config.xml. Alternatively you can manually enter them after the = sign.
  52.  
  53. DBUSER="$(awk -F '[<>]' '/UserName/{print $3}' $CONFIGXML)"
  54. DBPASS="$(awk -F '[<>]' '/Password/{print $3}' $CONFIGXML)"
  55. CHANID=$1 && STARTTIME=$2
  56. Populate recording information from sql database
  57.  
  58. TITLE=$(mysql mythconverg --user=$DBUSER --password=$DBPASS -se "SELECT title FROM recorded WHERE chanid=\"$CHANID\" AND starttime=\"$STARTTIME\";")
  59. SUBTITLE=$(mysql mythconverg --user=$DBUSER --password=$DBPASS -se "SELECT subtitle FROM recorded WHERE chanid=\"$CHANID\" AND starttime=\"$STARTTIME\";")
  60. DATE=$(mysql mythconverg --user=$DBUSER --password=$DBPASS -se "SELECT starttime FROM recorded WHERE chanid=\"$CHANID\" AND starttime=\"$STARTTIME\";")
  61. FILENAME=$(mysql mythconverg --user=$DBUSER --password=$DBPASS -se "SELECT basename FROM recorded WHERE chanid=\"$CHANID\" AND starttime=\"$STARTTIME\";")
  62. STORAGEGROUP=$(mysql mythconverg --user=$DBUSER --password=$DBPASS -se "SELECT storagegroup FROM recorded WHERE chanid=\"$CHANID\" AND starttime=\"$STARTTIME\";")
  63. DIRNAME=$(mysql mythconverg --user=$DBUSER --password=$DBPASS -se "SELECT dirname FROM storagegroup WHERE groupname=\"$STORAGEGROUP\";")
  64. SEASON=$(mysql mythconverg --user=$DBUSER --password=$DBPASS -se "SELECT season FROM recorded WHERE chanid=\"$CHANID\" AND starttime=\"$STARTTIME\";")
  65. EPISODE=$(mysql mythconverg --user=$DBUSER --password=$DBPASS -se "SELECT episode FROM recorded WHERE chanid=\"$CHANID\" AND starttime=\"$STARTTIME\";")
  66. FILEPATH="$DIRNAME$FILENAME"
  67. NEWNAME=$(echo ${CHANID}_${STARTTIME}).mkv
  68. NEWFILEPATH="$DIRNAME$NEWNAME"
  69. PRETTYNAME="$TITLE - s$SEASON""e$EPISODE - $SUBTITLE.mkv"
  70. PRETTYSUBDIR="$PRETTYDIRNAME$TITLE/"
  71. PRETTYFILEPATH="$PRETTYSUBDIR$PRETTYNAME"
  72. Flag commercials
  73.  
  74. mythcommflag --chanid "$CHANID" --starttime "$STARTTIME"
  75. Generate a cut list
  76.  
  77. mythutil --gencutlist --chanid "$CHANID" --starttime "$STARTTIME"
  78. Remove commercials from mpeg file
  79.  
  80. mythtranscode --chanid "$CHANID" --starttime "$STARTTIME" --mpeg2 --honorcutlist
  81. To fix seeking, we'll prune the database values containing the previous bookmarks.
  82.  
  83. mysql mythconverg --user=$DBUSER --password=$DBPASS -se "DELETE FROM recordedmarkup WHERE chanid=\"$CHANID\" AND starttime=\"$STARTTIME\";"
  84. mysql mythconverg --user=$DBUSER --password=$DBPASS -se "DELETE FROM recordedseek WHERE chanid=\"$CHANID\" AND starttime=\"$STARTTIME\";"
  85. Convert cut video to H264, preserving audio (and subtitles where supported.)
  86.  
  87. ffmpeg -i "$FILEPATH".tmp -c:v libx264 -preset $PRESET -crf $CRF -c:a copy -c:s copy -threads $THREADS -f matroska "$NEWFILEPATH"
  88. Update the file metadata to point to our newly cut and transcoded file.
  89.  
  90. NEWFILESIZE=du -b "$NEWFILEPATH" | cut -f1
  91. mysql mythconverg --user=$DBUSER --password=$DBPASS -se "UPDATE recorded SET basename=\"$NEWNAME\",filesize=\"$NEWFILESIZE\",transcoded=\"1\" WHERE chanid=\"$CHANID\" AND starttime=\"$STARTTIME\";"
  92. Delete the now useless files
  93.  
  94. rm "$FILEPATH" # I'm currently not deleting the old files, so that I can rerun the script if something goes wrong. Reenable this when all the bugs are hammered out.
  95.  
  96. rm "$FILEPATH".tmp
  97. create pretty name and path for file
  98.  
  99. mkdir -p "$PRETTYSUBDIR"
  100. if [[ -z SEASON ]]
  101. then
  102. mv "$NEWFILEPATH" "$PRETTYFILEPATH"
  103. mv "$FILEPATH".png "$PRETTYFILEPATH".png
  104. chown plex:media "$PRETTYFILEPATH"
  105. else
  106. mkdir -p "$PRETTYSUBDIR/Season $SEASON"
  107. mv "$NEWFILEPATH" "$PRETTYSUBDIR/Season $SEASON/$PRETTYNAME"
  108. mv "$FILEPATH".png "$PRETTYSUBDIR/Season $SEASON/$PRETTYNAME".png
  109. chown plex:media "$PRETTYSUBDIR/Season $SEASON/$PRETTYNAME"
  110. fi
  111. Prune all dead links and empty folders
  112.  
  113. find -L $PRETTYDIRNAME -type l -delete
  114. find $PRETTYDIRNAME -type d -empty -delete
  115. Notify Plex to refresh the library
  116.  
  117. curl "$PMSURL"library/sections/"$PMSSEC"/refresh
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement