修订版 | b21a7753674a96aa45ca0a53c572f47e63da31e7 (tree) |
---|---|
时间 | 2014-04-07 09:39:22 |
作者 | jakago <jakago777@gmai...> |
Commiter | jakago |
不要なコードを削除
@@ -16,6 +16,7 @@ require 'fileutils' | ||
16 | 16 | 善意を持って作成しておりますが、すべて使用される方の自己責任でお願いいたします。 |
17 | 17 | |
18 | 18 | ====================【更新履歴】==================== |
19 | +2014/04/07 不要なコードを削除。 | |
19 | 20 | 2014/04/06 Rubyの対応バージョンを2.0.0以降に変更。2014/03/31の仕様変更に対応。 |
20 | 21 | 「ニュースで英会話」と「ABCニュースシャワー」を削除。 |
21 | 22 | 2013/04/10 flvstreamerのダウンロード場所を更新。ffmpegのダウンロードに関する記述を追加。 |
@@ -88,12 +89,12 @@ require 'fileutils' | ||
88 | 89 | Mac OS X 10.9にrbenvを使って複数バージョンのRubyをインストールする - Qiita |
89 | 90 | http://qiita.com/ryam/items/33803f9a442399b60232 |
90 | 91 | ・ffmpeg |
91 | - http://www.evermeet.cx/ffmpeg/ffmpeg-2.2.7z | |
92 | + http://www.evermeet.cx/ffmpeg/ffmpeg-2.2.7z (もしくは最新版) | |
92 | 93 | 上記のファイルをブラウザでダウンロードして解凍した後、ファイル名をffmpegに変更し、ター |
93 | 94 | ミナルで実行属性を付け、パスの通ったディレクトリに置いてください。このバイナリはx86_64 |
94 | 95 | のため、i386またはppcバイナリが必要な場合はソースコードからビルドするか、パッケージマ |
95 | - ネージャ(Homebrew、MacPorts、Fink)でインストールしてください。CaptureStreamの最新版 | |
96 | - に含まれているものを利用することも可能です。 | |
96 | + ネージャ(Homebrew、MacPorts、Fink)でインストールしてください。Mac版のCaptureStream | |
97 | + 最新版に含まれているものを利用することも可能です。 | |
97 | 98 | |
98 | 99 | 【Linux環境】 |
99 | 100 | ・Ruby |
@@ -237,7 +238,7 @@ $ffmpeg_hash = { | ||
237 | 238 | } |
238 | 239 | |
239 | 240 | #-------------------------------------------------------------------------------- |
240 | -# 出力フォルダ名とmp3ファイル名のフォーマット文字列の解釈 | |
241 | +# 出力フォルダ名と出力ファイル名のフォーマット文字列の解釈 | |
241 | 242 | #-------------------------------------------------------------------------------- |
242 | 243 | |
243 | 244 | # 引数はxmlのhdateとfileアトリビュート |
@@ -344,200 +345,7 @@ def check_output_dir( dir_path ) | ||
344 | 345 | end |
345 | 346 | |
346 | 347 | #-------------------------------------------------------------------------------- |
347 | -# タグの書き込み | |
348 | -#-------------------------------------------------------------------------------- | |
349 | - | |
350 | -def encode_size( int32 ) | |
351 | - result = String.new() | |
352 | - result << ((int32 & (0x0000007f << 21)) >> 21).chr | |
353 | - result << ((int32 & (0x0000007f << 14)) >> 14).chr | |
354 | - result << ((int32 & (0x0000007f << 7)) >> 7).chr | |
355 | - result << (int32 & 0x0000007f).chr | |
356 | - return result | |
357 | -end | |
358 | - | |
359 | -# identifier and string must be UTF-8 | |
360 | -def ascii_frame( identifier, string ) | |
361 | - ascii_mark = "\x00" | |
362 | - result = String.new() | |
363 | - | |
364 | - if identifier.size == 3 && string.size > 0 | |
365 | - frame_data = NKF.nkf( '-l -W', string ) | |
366 | - if frame_data.size > 0 | |
367 | - frame_data = ascii_mark + frame_data + "\x00" | |
368 | - result << identifier | |
369 | - result << ((frame_data.size & 0x00ff0000) >> 16).chr | |
370 | - result << ((frame_data.size & 0x0000ff00) >> 8).chr | |
371 | - result << (frame_data.size & 0x000000ff).chr | |
372 | - result << frame_data | |
373 | - end | |
374 | - end | |
375 | - | |
376 | - return result | |
377 | -end | |
378 | - | |
379 | -# identifier and string must be UTF-8 | |
380 | -def unicode_frame( identifier, string ) | |
381 | - unicode_mark = "\x01" | |
382 | - result = String.new() | |
383 | - | |
384 | - if identifier.size == 3 && string.size > 0 | |
385 | - frame_data = NKF.nkf( '-w16L -W', string ) | |
386 | - if frame_data.size > 0 | |
387 | - frame_data = unicode_mark + frame_data + "\x00\x00" | |
388 | - result << identifier | |
389 | - result << ((frame_data.size & 0x00ff0000) >> 16).chr | |
390 | - result << ((frame_data.size & 0x0000ff00) >> 8).chr | |
391 | - result << (frame_data.size & 0x000000ff).chr | |
392 | - result << frame_data | |
393 | - end | |
394 | - end | |
395 | - | |
396 | - return result | |
397 | -end | |
398 | - | |
399 | -def create_tag( album, title, year, artist ) | |
400 | - tag_bytes = String.new() | |
401 | - frames = String.new() | |
402 | - | |
403 | - frames << unicode_frame( 'TAL', album ); | |
404 | - frames << unicode_frame( 'TT2', title ); | |
405 | - frames << ascii_frame( 'TYE', year ); | |
406 | - frames << unicode_frame( 'TP1', artist ); | |
407 | - frames << ascii_frame( 'TCO', '(101)' ); | |
408 | - | |
409 | - if frames.size > 0 | |
410 | - tag_bytes << "ID3\x02\x00\x00" | |
411 | - tag_bytes << encode_size( frames.size ) | |
412 | - tag_bytes << frames | |
413 | - end | |
414 | - | |
415 | - return tag_bytes | |
416 | -end | |
417 | - | |
418 | -def decode_size( byte4 ) | |
419 | - return ((byte4[0] & 0x7f) << 21) + ((byte4[1] & 0x7f) << 14) + | |
420 | - ((byte4[2] & 0x7f) << 7) + (byte4[3] & 0x7f) | |
421 | -end | |
422 | - | |
423 | -# 2.2.0~2.4.0のタグのサイズを計算する | |
424 | -def tag_size( buffer ) | |
425 | - #offset_identifier = 0 | |
426 | - offset_version = 3 | |
427 | - #offset_flags = 5 | |
428 | - offset_size = 6 | |
429 | - offset_data = 10 | |
430 | - identifier = 'ID3' | |
431 | - | |
432 | - result = 0 | |
433 | - | |
434 | - if buffer.size > offset_data && buffer[0,3] == identifier | |
435 | - # ID3v2.2.0 と ID3v2.3.0、ID3v2.4.0 のみサポート | |
436 | - if buffer[offset_version] >= '2'[0] && buffer[offset_version] <= '4'[0] && buffer[offset_version + 1] == '0'[0] | |
437 | - result = decode_size( buffer[offset_size,4] ) + offset_data | |
438 | - end | |
439 | - end | |
440 | - | |
441 | - return result; | |
442 | -end | |
443 | - | |
444 | -def make_temp_name( original ) | |
445 | - temp_file = Tempfile.open( File.basename( original ) ) | |
446 | - result = temp_file.path | |
447 | - temp_file.close! | |
448 | - return result | |
449 | -end | |
450 | - | |
451 | -def id3tag( full_path, album, title, year ) # full_pathはto_nativeで変換されている | |
452 | - src_file = nil | |
453 | - dst_file = nil | |
454 | - original_file_moved = false | |
455 | - | |
456 | - begin | |
457 | - tag_bytes = create_tag( album, title, year, 'NHK' ) | |
458 | - raise '書き込むべきタグが見当たらないため、タグの書き込みを中止します。' if tag_bytes.size <= 0 | |
459 | - temp_name = make_temp_name( full_path ) | |
460 | - raise '作業用ファイル名が作成できないため、タグの書き込みを中止します。' if temp_name.size <= 0 | |
461 | - FileUtils.move( full_path, temp_name ) | |
462 | - original_file_moved = true | |
463 | - src_file = File.open( temp_name, 'r' ) | |
464 | - src_file.binmode | |
465 | - dst_file = File.open( full_path, 'w' ) | |
466 | - dst_file.binmode | |
467 | - raise '作業用ファイルへの書き込みに失敗しました。' if dst_file.write( tag_bytes ) != tag_bytes.size | |
468 | - buffer = src_file.read | |
469 | - skip = tag_size( buffer ) | |
470 | - raise '作業用ファイルへの書き込みに失敗しました。' if dst_file.write( buffer[skip..-1] ) != buffer.size - skip | |
471 | - dst_file.close | |
472 | - dst_file = nil | |
473 | - src_file.close | |
474 | - File.unlink( temp_name ) | |
475 | - src_file = nil | |
476 | - rescue | |
477 | - jputs( $! ) | |
478 | - ( src_file.close if src_file ) rescue false | |
479 | - ( dst_file.close if dst_file ) rescue false | |
480 | - ( File.unlink( full_path ) if dst_file ) rescue false | |
481 | - ( FileUtils.move( temp_name, full_path ) if original_file_moved ) rescue false | |
482 | - end | |
483 | -end | |
484 | - | |
485 | -#-------------------------------------------------------------------------------- | |
486 | -# flvからmp3への変換 | |
487 | -#-------------------------------------------------------------------------------- | |
488 | - | |
489 | -FLV_HEADER_SIZE = 9 | |
490 | -FLV_TAG_SIZE = 15 | |
491 | -FLV_HEADER = Struct.new( :signature, :version, :flags, :offset ) | |
492 | -FLV_TAG = Struct.new( :previousTagSize, :type, :bodyLength, :timestamp, :timestampExtended, :streamId ) | |
493 | - | |
494 | -def flv2mp3( flv_path, mp3_path ) # flv_pathとmp3_pathはto_nativeで変換されている | |
495 | - result = false | |
496 | - | |
497 | - begin | |
498 | - # Windowsのためにバイナリモードを指定しなければならない | |
499 | - port = open( flv_path, 'rb' ) | |
500 | - begin | |
501 | - flv = port.read | |
502 | - ensure | |
503 | - port.close | |
504 | - end | |
505 | - header = FLV_HEADER.new( flv[0,3], flv[3], flv[4], flv[5, 4] ) | |
506 | - | |
507 | - raise 'flvファイルにヘッダが含まれていません。' if flv.length < FLV_HEADER_SIZE | |
508 | - raise 'flvファイルではありません。' unless header.signature == 'FLV' | |
509 | - raise '音声データが含まれていません。' if (header.flags & 4) == 0 | |
510 | - raise 'flvファイルが対応できる形式ではありません。' unless header.offset == "\x00\x00\x00#{FLV_HEADER_SIZE.chr}" | |
511 | - | |
512 | - read_size = FLV_HEADER_SIZE | |
513 | - | |
514 | - open( mp3_path, 'wb' ) { |mp3| | |
515 | - while true | |
516 | - remaining = flv.length - read_size | |
517 | - break if remaining == 4 # 最後のPreviousTagSize => 完了 | |
518 | - raise 'flvファイルの内容が不正です。' if remaining < FLV_TAG_SIZE | |
519 | - tag = FLV_TAG.new( flv[read_size, 4], flv[read_size + 4], flv[read_size + 5, 3], flv[read_size + 8, 3], flv[read_size + 11], flv[read_size + 12, 3] ) | |
520 | - read_size += FLV_TAG_SIZE | |
521 | - body_length = (tag.bodyLength[0] << 16) + (tag.bodyLength[1] << 8) + tag.bodyLength[2]; | |
522 | - raise 'flvファイルの内容が不正です。' if remaining < body_length | |
523 | - if tag.type == 8 | |
524 | - raise '音声データがmp3ではありません。' if (flv[read_size] & 0x00f0) != 0x20 | |
525 | - raise 'mp3ファイルの書き込みに失敗しました。' if mp3.write( flv[read_size + 1, body_length - 1] ) != body_length - 1 | |
526 | - end | |
527 | - read_size += body_length | |
528 | - end | |
529 | - } | |
530 | - result = true | |
531 | - rescue | |
532 | - puts( to_native( $!.to_s + ':' ) + flv_path ) | |
533 | - File.delete( mp3_path ) if File.exist?( mp3_path ) | |
534 | - end | |
535 | - | |
536 | - return result | |
537 | -end | |
538 | - | |
539 | -#-------------------------------------------------------------------------------- | |
540 | -# flvのダウンロードとmp3への変換 | |
348 | +# 音声ファイルのダウンロード | |
541 | 349 | #-------------------------------------------------------------------------------- |
542 | 350 | |
543 | 351 | def capture_stream( target, kouza, hdate, file ) |
@@ -590,7 +398,7 @@ end | ||
590 | 398 | # メインプログラム |
591 | 399 | #-------------------------------------------------------------------------------- |
592 | 400 | |
593 | -jputs( '語学講座ダウンローダ (2014/04/06)' ) | |
401 | +jputs( '語学講座ダウンローダ (2014/04/07)' ) | |
594 | 402 | |
595 | 403 | Dir.chdir( to_native( File.dirname( $script_path ) ) ) |
596 | 404 | targets = ARGV.length > 0 ? ARGV : $default_target |