rokkonet

PC・Androidソフトウェア・アプリの開発・使い方に関するメモ

(EPGRECでのテレビ録画)MPEG2 TSファイルをH.264に変換し重複ファイルを削除するbashシェルスクリプト

2021 Mar. 14.
2020 Oct. 17.
2020 Apr. 19.
2020 Apr. 18.

DIRディレクトリ内のテレビ録画tsファイルのH.264変換・重複ファイル削除シェルスクリプト

compress-epgrec-videos.sh DIR


compress-epgrec-videos.sh

#!/bin/bash
# 2020 Oct. 10.
# Ryuichi Hashimoto.

# Convert MPEG2video-files of EPGREC-type-filename in Dir to MPEG4s and delete redundunt files in Dir.
#   STANDARD-OUT: depends on tss2h264-ffmpeg.sh
#   Specifications of converted files: depend on tss2h264-ffmpeg.sh

# EPGREC-type-filename: ^20\d{12}_20\d{12}(GR|BS)CHAN.EXT
#   20170923012300_20170923022200GR22.ts
#   20170923210000_20170923215900BS103.ts

# Programs below that I wrote are required.
#   delredundrecfiles-epgrecfilenamematch.rb
#   Videofile.rb
#   tss2h264s-ffmpeg.sh
#   ispts.sh


CommandName=`\basename $0`
DelRedund='delredundrecfiles-epgrecfilenamematch.rb'
Tss='tss2h264s-ffmpeg.sh'

# Set commands that you do not want to run together with this script.
#   If these commands are running, this program is aborted.
# RelatedCommands='PROGRAM1|PROGRAM2'
RelatedCommands='ts2h264|delredundrecfiles|HandBrakeCLI'

function usage() {
\cat << EOP
usage: ${CommandName} Dir
  Convert MPEG2video-files of EPGREC-type-filename in Dir to MPEG4s and delete r
edundunt files in Dir.
    STANDARD-OUT: depends on tss2h264-ffmpeg.sh
    Specifications of converted files: depend on tss2h264-ffmpeg.sh

  EPGREC-type-filename: ^20\d{12}_20\d{12}(GR|BS)CHAN.EXT
    20170923012300_20170923022200GR22.ts
    20170923210000_20170923215900BS103.ts

  Programs below that I wrote are required.
    delredundrecfiles-epgrecfilenamematch.rb
    Videofile.rb
    tss2h264s-ffmpeg.sh
    ispts.sh
EOP
}


############
### main ###
############
if [ $# -ne 1 ]; then
  usage 1>&2
  \exit 1
fi

FileDir=$1

# convert relative path to absolute path
HeadLetter=${FileDir:0:1}
if [[ '/' != $HeadLetter ]]; then
  # $1 is relative path
  Wd=`pwd`
  FileDir=${Wd}/${FileDir}
fi
FileDir=${FileDir%/}
if [ ! -r ${FileDir} ]; then
  \echo "${FileDir} does not exist or is not readable.." 1>&2
  \exit 1
fi

# Set delete-command of redundent files
\which ${DelRedund} > /dev/null
Result=$?
if [ $Result -gt 0 ]; then
  \echo "${DelRedund} is not installed." 1>&2
  \exit 1
fi
DelRedundCom="nohup ${DelRedund} -d ${FileDir} < /dev/null > ${FileDir}/delredund-stdout.log 2> ${FileDir}/delredund-stderr.log &"

# Set H264-convert-command
\which ${Tss} > /dev/null
Result=$?
if [ $Result -gt 0 ]; then
  \echo "${Tss} is not installed." 1>&2
  \exit 1
fi
TssCom="nohup ${Tss} < /dev/null > tss2h264s-stdout.log 2> tss2h264s-stderr.log &"

# Run deleteRedundent-file-command/convert-file-command if related commands are not running.
\ispts.sh | \grep -E "${RelatedCommands}"
Ispt="$?"
   # Ispt 0: related commands are running.
   #      1: related commands are not running.

if [ 1 -eq ${Ispt} ]; then 
  cd ${FileDir}
  LastFinishedCmd=`\ls -lt delredund-std* tss2h264s-std* | \head -n 1 | \awk '{print $9}'`
  case ${LastFinishedCmd} in
     *tss*)
       \eval ${DelRedundCom} ;;
     *)
       \eval ${TssCom} ;;
  esac
  \exit 0
else
   \echo "Related commands are running." 1>&2
   \echo "Aborted." 1>&2
   \exit 1
fi
\exit 0


delredundrecfiles-epgrecfilenamematch.rb :
同じEPGREC録画ファイル名形式の複数ファイルから最小サイズを残し、他は削除するrubyスクリプト - rokkonet


Videofile.rb :
EPGREC録画動画ファイルを扱うrubyユーティリティ - rokkonet


tss2h264s-ffmpeg.sh :
MPEG2動画ファイルをH.264に変換するシェルスクリプト - rokkonet


ispts.sh :
スクリプト内で指定したコマンドが稼働中か調べるシェルスクリプト - rokkonet