Advertisement
Guest User

Untitled

a guest
Jul 19th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. # generatevideothumbs.pl
  4.  
  5. # this script will loop through all videos in videometadata,
  6. # capture a screenshot for each, move each screenshot to the
  7. # COVERPATH dir, and update each row in videometadata with
  8. # the appropriate information. Only rows with coverfile=
  9. # 'No Cover' are processed. A percentage of the length
  10. # of each video is used for the capture position to account
  11. # for clips of widely varying lengths.
  12.  
  13. # This script is mostly based on a bash script posted to the mythtv-users
  14. # mailing list by damonkeiman-at-hotmail-dot-com; viewable at:
  15. # <http://www.gossamer-threads.com/lists/mythtv/users/273113#273113>.
  16. # I had trouble getting it to work as-is (it kept losing parts of the output
  17. # from the first mysql query), and wound up rewriting it in Perl to get it to
  18. # work.
  19.  
  20. # Portions written by me are
  21. # Copyright (C) 2007 David Miller <dave@justdave.net>
  22. #
  23. # This program is free software: you can redistribute it and/or modify
  24. # it under the terms of the GNU General Public License as published by
  25. # the Free Software Foundation, either version 3 of the License, or
  26. # (at your option) any later version.
  27. #
  28. # This program is distributed in the hope that it will be useful,
  29. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  31. # GNU General Public License for more details.
  32. #
  33. # The full text of the GNU General Public License can be viewed at
  34. # <http://www.gnu.org/licenses/>.
  35.  
  36. use DBI;
  37.  
  38. if (getpwuid($>) ne "mythtv") {
  39. print STDERR "You must run this script as the mythtv user.\n";
  40. exit 1;
  41. };
  42.  
  43. # set some defaults
  44. my %config = (
  45. DBHostName => 'localhost',
  46. DBUserName => 'mythtv',
  47. DBName => 'mythconverg',
  48. DBPassword => '',
  49. );
  50.  
  51. # read in the real ones
  52. open MYSQLDATA, "<", "/etc/mythtv/mysql.txt";
  53. while (my $line = <MYSQLDATA>) {
  54. if ($line =~ /^(DB\S+)=(.*)\s*$/) {
  55. $config{$1} = $2;
  56. }
  57. }
  58. close MYSQLDATA;
  59.  
  60. # intialize variables
  61. my $OUTPATH='/tmp';
  62. my $COVERPATH='/home/mythtv/.mythtv/MythVideo';
  63. my $FRAME='00000002.jpg';
  64. my $SKIPOFFSET='35';
  65.  
  66. my $dsn = "DBI:mysql:host=" . $config{DBHostName} . ":database=" . $config{DBName};
  67. my $dbh = DBI->connect($dsn, $config{DBUserName}, $config{DBPassword});
  68.  
  69. my $sth = $dbh->prepare("SELECT intid, filename FROM videometadata WHERE coverfile='No Cover'");
  70. $sth->execute();
  71. while (my ($ID, $FILE) = $sth->fetchrow_array()) {
  72. print "MYSQLRESULT: id=$ID, file=$FILE\n";
  73.  
  74. # if video is mpeg, use ffmpeg12 codec to avoid pixelation
  75. my @VC = ();
  76. if ($FILE =~ /.*\.mp[e]*g/) {
  77. @VC=("-vc","ffmpeg12");
  78. }
  79.  
  80. # if video is a .iso or a .img, add dvd:\\1 -dvd-device
  81. my @dvd = ();
  82. if ($FILE =~ /\.(iso|img)$/i) {
  83. @dvd=("dvd://1", "-dvd-device");
  84. }
  85.  
  86. # use mplayer to get length of video in seconds
  87. my $length=`echo -n q | mplayer -vo null -nosound -identify -ac null -vc null "$FILE" | egrep "^(ID_LENGTH=)" | cut -d '=' -f 2`;
  88.  
  89. if (!$length) {
  90. print "mplayer was unable to get the $FILE length.\n";
  91. next;
  92. }
  93.  
  94. my $SKIP = int(($length * $SKIPOFFSET) / 100);
  95. print "calculated skip seconds: $SKIP\n";
  96.  
  97. # command to get jpeg from video
  98. system("mplayer","-really-quiet","-ac","null","-noframedrop","-ss",$SKIP,"-vo","jpeg:quality=50:outdir=$OUTPATH","-frames","2",@VC,"-nosound",@dvd,$FILE);
  99.  
  100. # move jpeg from outpath to coverpath
  101. print "mv $OUTPATH/$FRAME $COVERPATH/intid.$ID.jpg\n";
  102. if (0 != system("mv", "$OUTPATH/$FRAME", "$COVERPATH/intid.$ID.jpg")) {
  103. print "Move failed. Will not update videometadata table...\n";
  104. unlink "$OUTPATH/$FRAME";
  105. }
  106. else {
  107. # SQL to update table with cover image path
  108. my $usth = $dbh->prepare("UPDATE videometadata SET coverfile='$COVERPATH/intid.$ID.jpg' WHERE intid=$ID LIMIT 1");
  109. $usth->execute();
  110. }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement