Last active
March 6, 2018 09:21
-
-
Save highgain86j/816479e30bd058dec07c52d49b31c7c5 to your computer and use it in GitHub Desktop.
A bash script with which you should be able to import MTS files to Ubuntu/Debian from AVCHD camcoders.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
MTS_LIST=`mktemp /tmp/mts.XXXXXX` | |
THREADS_TOTAL=(`cat /proc/cpuinfo | egrep '^processor' | awk '{print $3}'`) | |
SPANNED_FILE_SIZE=4288180224 #This number varies by camera. This value is optimized for Panasonic HDC-HS100. | |
function func_new_thread(){ | |
local tmp_list=`mktemp /tmp/list.XXXXXX` || exit 1 | |
local tmp_cat=`mktemp /tmp/cat.XXXXXX` || exit 1 | |
# Create FIFO to allow input from multiple MTS files at once | |
local tmp_fifo=`mktemp /tmp/fifo.XXXXXX` || exit 1 | |
rm ${tmp_fifo} | |
mkfifo ${tmp_fifo} | |
local line | |
local ffmpeg_instances=() | |
cat ${MTS_LIST} | grep ${1} | awk '{print $2}' > ${tmp_list} | |
# Build cat script as input to fifo | |
echo -n "cat " > ${tmp_cat} | |
cat ${tmp_list} | while read line; do | |
echo "\"${line}\" \\" >> ${tmp_cat} | |
done | |
echo "> ${tmp_fifo} &" >> ${tmp_cat} | |
chmod +x ${tmp_cat} | |
${tmp_cat} | |
ffmpeg_instances=(`pidof ffmpeg`) | |
while :; do | |
if [ ${#ffmpeg_instances[@]} -lt 2 ]; then | |
echo "Launching a job with ID:${1}." | |
nice -n 19 ffmpeg -loglevel fatal -i "${tmp_fifo}" -vcodec copy -acodec copy -threads ${#THREADS_TOTAL[@]} ~/${1}.MTS | |
rm ${tmp_list} | |
rm ${tmp_cat} | |
rm ${tmp_fifo} | |
echo "Completed a job with ID:${1}." | |
break | |
else | |
sleep 1 | |
ffmpeg_instances=(`pidof ffmpeg`) | |
fi | |
done | |
} | |
if [ "`which ffmpeg`" == "" ]; then | |
echo -e "This script requires \"ffmpeg\" installed. " | |
exit 1 | |
fi | |
# Detect USB storage | |
ARR_USB_STORAGE=(`dmesg | grep "Attached SCSI removable disk" | awk '{print $5}' | egrep '^\[' | sort | uniq | sed -e 's/\[//g' -e 's/\]//g'`) | |
if [ ${#ARR_USB_STORAGE[@]} -eq 0 ]; then | |
exit 1 | |
else | |
# Detect USB storage that are actually mounted | |
ARR_USB_STORAGE_MOUNTED=(`for ((COUNT = 0; COUNT < ${#ARR_USB_STORAGE[@]}; COUNT++)); do mount | grep ${ARR_USB_STORAGE[${COUNT}]}; done | awk '{print $3}'`) | |
if [ ${#ARR_USB_STORAGE_MOUNTED[@]} -eq 0 ]; then | |
echo "Detected no USB mass storage (mounted)." | |
exit 1 | |
else | |
for ((COUNT = 0; COUNT < ${#ARR_USB_STORAGE_MOUNTED[@]}; COUNT++)); do | |
echo "Detected a USB mass storage (mounted) at ${ARR_USB_STORAGE_MOUNTED[${COUNT}]}." | |
# Detect locations of MTS files within the mounted USB storage | |
ARR_MTS_LOCATION=("${ARR_MTS_LOCATION[@]}" `find ${ARR_USB_STORAGE_MOUNTED[${COUNT}]} -type f | egrep '\.MTS$|\.mts$' | while read LINE; do dirname "${LINE}";done | uniq`) | |
done | |
# Detect file-spanning | |
if [ ${#ARR_MTS_LOCATION[@]} -eq 0 ]; then | |
exit 1 | |
else | |
for ((COUNT = 0; COUNT < ${#ARR_MTS_LOCATION[@]}; COUNT++)); do | |
echo "MTS files have been detected at ${ARR_MTS_LOCATION[${COUNT}]}." | |
done | |
CLIP_NUMBER="`date +"%Y%m%d%H%M"`00" | |
for ((COUNT = 0; COUNT < ${#ARR_MTS_LOCATION[@]}; COUNT++)); do | |
LANG=C;ls -l ${ARR_MTS_LOCATION[${COUNT}]} | egrep '^-' | egrep '\.MTS|\.mts' | awk '{print $9" "$5}' | while read LINE; do | |
FILE_NAME=`echo ${LINE} | awk '{print $1}'` | |
FILE_SIZE=`echo ${LINE} | awk '{print $2}'` | |
echo "${CLIP_NUMBER} ${ARR_MTS_LOCATION[${COUNT}]}/${FILE_NAME}" >> ${MTS_LIST} | |
if [ ${FILE_SIZE} -lt ${SPANNED_FILE_SIZE} ]; then | |
CLIP_NUMBER=`expr ${CLIP_NUMBER} + 1` | |
fi | |
done | |
done | |
ARR_CLIP_ID=(`cat ${MTS_LIST} | awk '{print $1}' | sort | uniq`) | |
if [ ${#ARR_CLIP_ID[@]} -eq 0 ]; then | |
JOB_RUNNING=false | |
exit 1 | |
else | |
for ((COUNT = 0; COUNT < ${#ARR_CLIP_ID[@]}; COUNT++)); do | |
(func_new_thread ${ARR_CLIP_ID[${COUNT}]})& | |
JOB_RUNNING=true | |
done | |
fi | |
fi | |
fi | |
fi | |
while ${JOB_RUNNING}; do | |
if [ "`find /tmp -maxdepth 1 -type f | egrep '\/list\.[a-z,A-Z,0-9]+$|\/cat\.[a-z,A-Z,0-9]+$|\/fifo\.[a-z,A-Z,0-9]+$'`" == "" ]; then | |
JOB_RUNNING=false | |
else | |
sleep 5 | |
fi | |
done | |
rm ${MTS_LIST} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment