Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
613
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 10.04 KB | None | 0 0
  1. #!/usr/bin/perl
  2. # skype-record - a wrapper script that allows you to record a skype conversation
  3. #
  4. # Copyright (C) 2005 Marilen Corciovei <len@nemesisit.ro>
  5. # Converted to Perl and expanded by Nathan Poznick <kraken@wang-fu.org>
  6. #
  7. # Expanded with the option of encoding to ogg-vorbis, global/absolute files
  8. # paths, date-based filenaming and forked converting so that files can be
  9. # converted after hangup while skype is still running.
  10. # By Benjamin Vestergaard <der_phox@gmx.net>
  11. #
  12. #   Based on the vsound wrapper script.  vsound's copyright:
  13. #   Copyright (C) 2004 Nathan Chantrell <nsc@zorg.org>
  14. #   Copyright (C) 2003 Richard Taylor <r.taylor@bcs.org.uk>
  15. #   Copyright (C) 2000,2001 Erik de Castro Lopo <erikd@zip.com.au>
  16. #   Copyright (C) 1999 James Henstridge <james@daa.com.au>
  17. #  
  18. # This program is free software; you can redistribute it and/or modify
  19. # it under the terms of the GNU Lesser General Public License as published by
  20. # the Free Software Foundation; either version 2.1 of the License, or
  21. # (at your option) any later version.
  22. #
  23. # This program is distributed in the hope that it will be useful,
  24. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  26. # GNU Lesser General Public License for more details.
  27. #
  28. # You should have received a copy of the GNU Lesser General Public License
  29. # along with this program; if not, write to the Free Software
  30. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  31.  
  32. require $ENV{'HOME'}."/.skype-rec";
  33.  
  34. our ($SKYPE_REC, $SKYPE, $SOX, $SOXMIX, $ENC_TYPE, $LAME, $LAME_OPTS,
  35.      $OGGENC, $OGGENC_OPTS, $OUTPUT_DIR, $STEREO_MIX, $SORT_INTO_DIRS,
  36.      $CLEANUP_TEMP, $CLEANUP_ORIG, $CHECK_INTERVAL);
  37.  
  38. use strict;
  39.  
  40. unless (defined($SKYPE_REC)) {
  41.     print "ERROR: Missing configuration setting: \$SKYPE_REC\n";
  42.     exit 1;
  43. }
  44. $ENV{'LD_PRELOAD'}=$SKYPE_REC;
  45. my $err = `ls 2>&1`;
  46. delete $ENV{'LD_PRELOAD'};
  47. if ($err =~ /ERROR: ld\.so: object '.*?' from LD_PRELOAD cannot be preloaded/) {
  48.     print "ERROR: It doesn't appear that we can find libskype-rec.so at ".
  49.         "'$SKYPE_REC'.  Please check your configuration and try again.\n";
  50.     exit 1;
  51. }
  52. unless (defined($SKYPE)) {
  53.     print "ERROR: Missing configuration setting: \$SKYPE\n";
  54.     exit 1;
  55. }
  56. `which $SKYPE`;
  57. if ($? != 0) {
  58.     print "ERROR: Couldn't locate skype binary at '$SKYPE'";
  59.     exit 1;
  60. }
  61. unless (defined($SOX)) {
  62.     print "ERROR: Missing configuration setting: \$SOX\n";
  63.     exit 1;
  64. }
  65. `which $SOX`;
  66. if ($? != 0) {
  67.     print "ERROR: Couldn't locate sox binary at '$SOX'";
  68.     exit 1;
  69. }
  70. unless (defined($SOXMIX)) {
  71.     print "ERROR: Missing configuration setting: \$SOXMIX\n";
  72.     exit 1;
  73. }
  74. `which $SOXMIX`;
  75. if ($? != 0) {
  76.     print "ERROR: Couldn't locate soxmix binary at '$SOXMIX'";
  77.     exit 1;
  78. }
  79. unless (defined($ENC_TYPE)) {
  80.     print "ERROR: Missing configuration setting: \$ENC_TYPE\n";
  81.     exit 1;
  82. }
  83. unless ($ENC_TYPE == 0 || $ENC_TYPE == 1 || $ENC_TYPE == 2) {
  84.     print "ERROR: Invalid value for \$ENC_TYPE\n";
  85.     exit 1;
  86. }
  87. unless (defined($LAME)) {
  88.     print "ERROR: Missing configuration setting: \$LAME\n";
  89.     exit 1;
  90. }
  91. if ($ENC_TYPE == 2) {
  92.     `which $LAME`;
  93.     if ($? != 0) {
  94.         print "ERROR: Couldn't locate lame binary at '$LAME'";
  95.         exit 1;
  96.     }
  97. }
  98. unless (defined($LAME_OPTS)) {
  99.     print "ERROR: Missing configuration setting: \$LAME_OPTS\n";
  100.     exit 1;
  101. }
  102. unless (defined($OGGENC)) {
  103.     print "ERROR: Missing configuration setting: \$OGGENC\n";
  104.     exit 1;
  105. }
  106. if ($ENC_TYPE == 1) {
  107.     `which $OGGENC`;
  108.     if ($? != 0) {
  109.         print "ERROR: Couldn't locate oggenc binary at '$OGGENC'";
  110.         exit 1;
  111.     }
  112. }
  113. unless (defined($OGGENC_OPTS)) {
  114.     print "ERROR: Missing configuration setting: \$OGGENC_OPTS\n";
  115.     exit 1;
  116. }
  117. unless (defined($OUTPUT_DIR)) {
  118.     print "ERROR: Missing configuration setting: \$OUTPUT_DIR\n";
  119.     exit 1;
  120. }
  121. unless (-d $OUTPUT_DIR) {
  122.     if (-e $OUTPUT_DIR) {
  123.         print "ERROR: Output directory $OUTPUT_DIR exists but is not a directory\n";
  124.         exit 1;
  125.     }
  126.     `mkdir -p $OUTPUT_DIR`;
  127.     if ($? != 0) {
  128.         print "ERROR: Could not create missing output directory $OUTPUT_DIR : $!\n";
  129.         exit 1;
  130.     }
  131.     else {
  132.         print "NOTICE: Created non-existent output directory $OUTPUT_DIR\n";
  133.     }
  134.     unless (-W $OUTPUT_DIR) {
  135.         print "ERROR: $OUTPUT_DIR is not writable by the current user\n";
  136.         exit 1;
  137.     }
  138. }
  139. unless (defined($STEREO_MIX)) {
  140.     print "ERROR: Missing configuration setting: \$STEREO_MIX\n";
  141.     exit 1;
  142. }
  143. unless (defined($SORT_INTO_DIRS)) {
  144.     print "ERROR: Missing configuration setting: \$SORT_INTO_DIRS\n";
  145.     exit 1;
  146. }
  147. unless (defined($CLEANUP_TEMP)) {
  148.     print "ERROR: Missing configuration setting: \$CLEANUP_TEMP\n";
  149.     exit 1;
  150. }
  151. unless (defined($CLEANUP_ORIG)) {
  152.     print "ERROR: Missing configuration setting: \$CLEANUP_ORIG\n";
  153.     exit 1;
  154. }
  155. unless (defined($CHECK_INTERVAL)) {
  156.     print "ERROR: Missing configuration setting: \$CHECK_INTERVAL\n";
  157.     exit 1;
  158. }
  159.  
  160.  
  161. my $convert_run = 1;
  162. my $type;
  163. if ($ENC_TYPE == 0) {
  164.     $type = "wav";
  165. }
  166. elsif ($ENC_TYPE == 1) {
  167.     $type = "ogg";
  168. }
  169. elsif ($ENC_TYPE == 2) {
  170.     $type = "mp3";
  171. }
  172.  
  173. print "Starting file conversion process...\n";
  174. my $pid = fork;
  175. if (!$pid) {
  176.     $SIG{TERM} = sub { $convert_run = 0; };
  177.     while ($convert_run) {
  178.         sleep $CHECK_INTERVAL;
  179.         &check_for_files;
  180.     }
  181.     exit 0;
  182. }
  183.  
  184. print "Running skype...\n";
  185. $ENV{'LD_PRELOAD'} = $SKYPE_REC;
  186. $ENV{'SKYPE_REC_DIR'} = $OUTPUT_DIR;
  187. system $SKYPE;
  188. delete $ENV{'SKYPE_REC_DIR'};
  189. delete $ENV{'LD_PRELOAD'};
  190. print "Please wait while conversion process finishes...\n";
  191. kill 'TERM', $pid;
  192. my $ret_pid = wait;
  193. if ($ret_pid == -1) {
  194.     print "ERROR: Couldn't find the conversion process, that's weird.\n";
  195. }
  196. print "Done\n";
  197. exit 0;
  198.  
  199. sub check_for_files {
  200.     my (@stamps, @files, $now_time);
  201.     $now_time = time;
  202.     opendir(DIR, $OUTPUT_DIR);
  203.     my @files = grep /remote-\d+\.au/, readdir(DIR);
  204.     closedir(DIR);
  205.     foreach (@files) {
  206.         if (-f "$OUTPUT_DIR/$_" && /remote-(\d+)\.au/) {
  207.             push @stamps, $1;
  208.         }
  209.     }
  210.     if (@stamps > 0) {
  211.         &convert_file(@stamps);
  212.     }
  213. }
  214.  
  215. sub convert_file {
  216.     my @stamps = @_;
  217.  
  218.     foreach my $stamp (@stamps) {
  219.         my $remote = "$OUTPUT_DIR/remote-$stamp.au";
  220.         my $local = "$OUTPUT_DIR/local-$stamp.au";
  221.         my $conv_base = "$OUTPUT_DIR/conversation-$stamp";
  222.  
  223.         my ($year, $month, $day, $hour, $min, $sec) =
  224.             $stamp =~ /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/;
  225.         my $pretty = "$year-$month-$day $hour:$min:$sec";
  226.  
  227.         next if ($SORT_INTO_DIRS && -e "$OUTPUT_DIR/$year/$month/$day/conversation-$stamp.$type");
  228.         next if (!$SORT_INTO_DIRS && -e "$conv_base.$type");
  229.  
  230.         print "$pretty: Converting conversation\n";
  231.  
  232.         unless (-e $remote && -e $local) {
  233.             print "$pretty: Couldn't find both $remote and $local to mix together\n";
  234.             next;
  235.         }
  236.  
  237.         my $tmp = -s $remote;
  238.         unless ($tmp != 0 && $tmp != 24) {
  239.             print "$pretty: Not mixing, $remote appears to be empty\n";
  240.             next;
  241.         }
  242.         $tmp = -s $local;
  243.         unless ($tmp != 0 && $tmp != 24) {
  244.             print "$pretty: Not mixing, $local appears to be empty\n";
  245.             next;
  246.         }
  247.  
  248.         if ($STEREO_MIX) {
  249.             system "$SOX $remote -c 2 $remote.TMP.au pan -1 >/dev/null 2>&1";
  250.             if ($? != 0) {
  251.                 print "$pretty: Error converting $remote to stereo\n";
  252.                 next;
  253.             }
  254.             system "$SOX $local -c 2 $local.TMP.au pan 1 >/dev/null 2>&1";
  255.             if ($? != 0) {
  256.                 print "$pretty: Error converting $local to stereo\n";
  257.                 next;
  258.             }
  259.             system "$SOXMIX -v 1 $remote.TMP.au -v 1 $local.TMP.au -v 1 $conv_base.au >/dev/null 2>&1";
  260.             if ($? != 0) {
  261.                 print "$pretty: Error mixing $remote and $local into $conv_base.au\n";
  262.                 next;
  263.             }
  264.             unlink "$remote.TMP.au", "$local.TMP.au" if ($CLEANUP_TEMP);
  265.         }
  266.         else {
  267.             system "$SOXMIX $remote $local $conv_base.au >/dev/null 2>&1";
  268.             if ($? != 0) {
  269.                 print "$pretty: Error mixing $remote and $local into $conv_base.au\n";
  270.                 next;
  271.             }
  272.         }
  273.         system "$SOX $conv_base.au $conv_base.wav >/dev/null 2>&1";
  274.         if ($? != 0) {
  275.             print "$pretty: Error converting $conv_base.au into $conv_base.wav\n";
  276.             next;
  277.         }
  278.         unlink "$conv_base.au" if ($CLEANUP_TEMP);
  279.         if ($ENC_TYPE == 1) {
  280.             system("$OGGENC $OGGENC_OPTS -o $conv_base.$type $conv_base.wav >/dev/null 2>&1");
  281.             unlink "$conv_base.wav" if ($CLEANUP_TEMP);
  282.         }
  283.         elsif ($ENC_TYPE == 2) {
  284.             system("$LAME $LAME_OPTS $conv_base.wav $conv_base.$type >/dev/null 2>&1");
  285.             unlink "$conv_base.wav" if ($CLEANUP_TEMP);
  286.         }
  287.         print "$pretty: Created $conv_base.$type\n";
  288.         unlink "$remote", "$local" if ($CLEANUP_ORIG);
  289.         if ($SORT_INTO_DIRS) {
  290.             if (-e "$OUTPUT_DIR/$year/$month/$day") {
  291.                 if (-d "$OUTPUT_DIR/$year/$month/$day") {
  292.                     `mv $conv_base.$type $OUTPUT_DIR/$year/$month/$day`;
  293.                     if ($? != 0) {
  294.                         print "$pretty: Couldn't move $conv_base.$type to $OUTPUT_DIR/$year/$month/$day\n";
  295.                         next;
  296.                     }
  297.                 }
  298.                 else {
  299.                     print "$pretty: $OUTPUT_DIR/$year/$month/$day exists but is not a".
  300.                         "directory, have you been screwing with things?\n";
  301.                     next;
  302.                 }
  303.             }
  304.             else {
  305.                 `mkdir -p $OUTPUT_DIR/$year/$month/$day`;
  306.                 if ($? != 0) {
  307.                     print "$pretty: Couldn't create directory $OUTPUT_DIR/$year/$month/$day\n";
  308.                     next;
  309.                 }
  310.                 `mv $conv_base.$type $OUTPUT_DIR/$year/$month/$day`;
  311.                 if ($? != 0) {
  312.                     print "$pretty: Couldn't move $conv_base.$type to $OUTPUT_DIR/$year/$month/$day\n";
  313.                     next;
  314.                 }
  315.             }
  316.         }
  317.     }
  318. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement