From 3365f5dc5216f77f9b7d0f54b1737420a77e0836 Mon Sep 17 00:00:00 2001 From: satomichan Date: Wed, 4 Sep 2024 06:16:51 +0900 Subject: [PATCH] =?utf8?q?=E6=9C=80=E5=88=9D=E3=81=AE=20version.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- rec-radiko.pl | 124 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 rec-radiko.pl diff --git a/rec-radiko.pl b/rec-radiko.pl new file mode 100644 index 0000000..6fdeee9 --- /dev/null +++ b/rec-radiko.pl @@ -0,0 +1,124 @@ +#!/usr/bin/env -S perl -w + +# 指定時刻に radiko を録音するための at job を発行するツール. +# (https://satomichan.jp/rec-radiko) +# +# 録音には radish (https://github.com/uru2/radish) を利用しています. +# m4a形式で録音後, mp3形式に変換をします. +# +# 使い方は引数なしで実行すると表示されます. +# +# Copyright 2024 FUKUDA Satomi (https://satomichan.jp/) +# +# Licensed under the Apache License, Version 2.0 (the “License”); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an “AS IS” BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# +# See the License for the specific language governing permissions and +# limitations under the License. + +use strict; +use warnings; + +use utf8; +binmode STDIN, ":utf8"; +binmode STDOUT, ":utf8"; +binmode STDERR, ":utf8"; + +use File::Basename; +use Getopt::Long qw(:config posix_default no_ignore_case gnu_compat); + + + +# コマンドのフルパスを取得 +my $AT = `which at | tr -d '\n'` or die 'at がインストールされていないか, パスが通っていません.'; +my $RADISH = `which radi.sh | tr -d '\n'` or die 'radi.sh がインストールされていないか, パスが通っていません.'; +my $FFMPEG = `which ffmpeg | tr -d '\n'` or die 'ffmpeg がインストールされていないか, パスが通っていません.'; +my $BASH = `which bash | tr -d '\n'`; +my $ECHO = `which echo | tr -d '\n'`; +my $SLEEP = `which sleep | tr -d '\n'`; +my $RM = `which rm | tr -d '\n'`; + + + +# 保存先 +my $DIR_SAVE_TO = $ENV{RADIKO_SAVE_TO}; +die "環境変数 RADIKO_SAVE_TO に保存先ディレクトリを指定してください." unless $DIR_SAVE_TO; +die "保存先 $DIR_SAVE_TO が存在しません." unless -d $DIR_SAVE_TO; +die "保存先 $DIR_SAVE_TO に書き込み権限がありません." unless -w $DIR_SAVE_TO; + + + +# 動作モード +my $is_check_mode = 0; +my $is_conv_to_mp3 = 1; +my $SLEEP_TIME_SEC = 5; + + + +# オプション解析 +my %opts; +GetOptions( \%opts, ("check|c", "no-conversion-to-mp3|n") ); +$is_check_mode = 1 if $opts{'check'}; +$is_conv_to_mp3 = 0 if $opts{'no-conversion-to-mp3'}; + + + +# 引数なし実行 使い方を表示 +unless (@ARGV) { + my $script_name = basename($0, ''); + my $usage = << " EOM_USAGE"; + USAGE) $script_name [-c|--check] [-n|--no-conversion-to-mp3] <放送局ID> <録音開始日> <録音開始時刻> <録音長(分)> <タイトル> + ex) $script_name JOAK-FM 2024-08-25 0305 56 shinyabin_taniyama-hiroko + ex) $script_name --check JOAK-FM 2024-08-25 3:05 56 shinyabin_taniyama-hiroko + + <放送局ID>は radi.sh -l | perl -ne 'print if /radiko/../^\$/' で確認できます. + + EOM_USAGE + + print STDERR $usage; + exit; +} + + + +# 引数 解析 +my($station, $date, $time, $min, $title) = @ARGV; + +my($y, $m, $d) = split(/-/, $date); +my($time_h, $time_m) = ($1, $2) if $time =~ /(\d{1,2}):?(\d{2})/; +$title =~ s/\s+/_/g; +die "無効な引数です." unless ($y && $m && $d && $time_h <= 24 && $time_h >= 0 && + $time_m <= 59 && $time_m >= 0 && $min > 0 && length $title); + +my $file_name_base = sprintf('%04d-%02d-%02d_%02d%02d_%s_%s', $y, $m, $d, $time_h, $time_m, $station, $title); +my $m4a = "${file_name_base}.m4a"; +my $mp3 = "${file_name_base}.mp3"; + + + +# コマンド構築 +my $rec_cmd = "cd $ENV{RADIKO_SAVE_TO} ; $RADISH -t radiko -s $station -d $min -o $m4a ; "; +if ($is_conv_to_mp3) { + $rec_cmd .= "$FFMPEG -i $m4a $mp3 &> /dev/null ; $SLEEP $SLEEP_TIME_SEC ; if [ -s $mp3 ]; then $RM $m4a; fi"; +} + +my $at_time = sprintf('%d:%02d %02d.%02d.%02d', $time_h, $time_m, $d, $m, ($y - int($y/100)*100 ) ); + +my $cmd = qq{$ECHO -e '$BASH << EOC\\n$rec_cmd\\nEOC' | $AT "$at_time"}; + + + +# 実行 +if ($is_check_mode) { + print "$cmd\n\n"; + exit; +} else { + system($cmd); + exit; +} -- 2.43.0