#!/bin/sh
# /media/bill/ATA_WDC_500G/Video production/170930 DrumLibrary - kids science/video production/convert MTS to ogv.sh
# www.BillHowell.ca   23Sep2017 based on :  0_vidExtrn - common data, operators.ndf
# For instructions, see /media/bill/VIDEO_PRODN/video capture/0_Howell - video capture instructions.odt


#avconv  -i  "Language translation experiment 09m51s.MTS"  "Language translation experiment 09m51s.ogv" 
# This doesn't run, gives error  : 
# Language translation experiment 09m51s.MTS: Invalid data found when processing input


# https://gist.github.com/yellowled/1439610  
#yellowled/ffmpeg-html5,  Created 6 years ago (~2011)
#Convert videos to proper formats for HTML5 video on Linux shell using ffmpeg. Will probably convert this to a bash script later, but for the time being, here's some examples. Not sure there have actually sensible dimensions and bitrates for web video. 
#  MrOrz commented on Jun 3, 2014 
# Thanks for sharing this script. Extremely useful!
# BTW I found that while single-pass webm video looks bad compared to h264 videos with the same size, its performance is 
# pretty satisfactory when using 2-pass encoding workflow.
#ffmpeg -i $(IN) -c:v libvpx -pass 1 -an -f rawvideo -y /dev/null  # Generates ffmpeg2pass-0.log
#ffmpeg -i $(IN) -c:v libvpx -pass 2 -f webm -b:v 400K -crf 10 -an -y $(OUT).webm

# https://wiki.archlinux.org/index.php/FFmpeg
# Two-pass x264 (very high-quality)
# Audio deactivated as only video statistics are recorded during the first of multiple pass runs:
# $ ffmpeg -i video.VOB -an -vcodec libx264 -pass 1  -preset veryslow \
# -threads 0 -b 3000k -x264opts frameref=15:fast_pskip=0 -f rawvideo -y /dev/null
# Container format is automatically detected and muxed into from the output file extenstion (.mkv):
# $ ffmpeg -i video.VOB -acodec libvo-aacenc -ab 256k -ar 96000 -vcodec libx264 \
# -pass 2 -preset veryslow -threads 0 -b 3000k -x264opts frameref=15:fast_pskip=0 video.mkv
# Tip: If you receive Unknown encoder 'libvo-aacenc' error (given the fact that your ffmpeg is compiled with libvo-aacenc enabled), you may want to try -acodec libvo_aacenc, an underscore instead of hyphen.

# Only a two-pass approach is shown, so ffmpeg doesn't do a third?
# Should I add back the "missing codes" like "-vcodec libx264"?  Problem is, I don't know codecs!
# 25Oct2017 First pass I added back 

# https://superuser.com/questions/624563/how-to-resize-a-video-to-make-it-smaller-with-ffmpeg
# The most basic example is this:
# ffmpeg -i input.avi -s 720x480 -c:a copy output.mkv
# Using the scale filter will provide more flexibility:
# ffmpeg -i input.avi -filter:v scale=720:-1 -c:a copy output.mkv
# The -1 will tell ffmpeg to automatically choose the correct height in relation to the provided width to preserve the aspect 
# ratio. -1 can also be used for width if you provide a given height.
# edited Jan 2 '14 at 10:45, slhck
# answered Nov 30 '12 at 6:53, jimhark

# HOWELL : first try -filter:v scale=<target width>:-1
# use error output from ffmpeg (if height is odd number) to set 

# Lenovo
#d_scenes="/media/bill/ATA_WDC_500G/Video production/170930 DrumLibrary - kids science/Scenes/"
# Dell64
d_scenes="/home/bill/Video production/170930 DrumLibrary - kids science/Scenes/"



# $1=d_Scenes   $2=fnameBase   $3=xSize (pixels)

convert_it()
{  
cd  "$d_scenes""$1"
ffmpeg -i "$2"".MTS"  -filter:v  scale=$3:-1  -an -vcodec libx264  -pass 1  -preset veryslow  -threads 0 -b 3000k -x264opts frameref=15:fast_pskip=0   -f rawvideo -y /dev/null
ffmpeg -i "$2"".MTS"  -filter:v  scale=$3:-1                       -pass 2  -preset veryslow  -threads 0 -b 3000k -x264opts frameref=15:fast_pskip=0 "$2"".ogv"
}  

# 24Oct2017 changed from : 
#ffmpeg -i "Language translation experiment 09m51s.MTS"  -c:v libvpx -pass 1  -b:v 400K -crf 10  "Language translation experiment 09m51s-1.ogv"


old_convert_it()
{  
# 24Oct2017 
cd  "$d_scenes""$1"
ffmpeg -i "$2"".MTS"  -pass 1  "$2"" 1.ogv"
ffmpeg -i "$2"".MTS"  -pass 2  "$2"" 2.ogv"
# 25Oct2017 
cd  "$d_scenes""$1"
ffmpeg -i "$2"".MTS"  -filter:v  scale=624:-1  -an -vcodec libx264  -pass 1  -preset veryslow  -threads 0 -b 3000k -x264opts frameref=15:fast_pskip=0   -f rawvideo -y /dev/null
ffmpeg -i "$2"".MTS"  -filter:v  scale=624:-1                       -pass 2  -preset veryslow  -threads 0 -b 3000k -x264opts frameref=15:fast_pskip=0 "$2"".ogv"
cd  "$d_scenes""$1"
ffmpeg -i "$2"".MTS"  625x351  -an -vcodec libx264  -pass 1  -preset veryslow  -threads 0 -b 3000k -x264opts frameref=15:fast_pskip=0   -f rawvideo -y /dev/null
ffmpeg -i "$2"".MTS"  625x351                       -pass 2  -preset veryslow  -threads 0 -b 3000k -x264opts frameref=15:fast_pskip=0 "$2"".ogv"
cd  "$d_scenes""$1"
ffmpeg -i "$2"".MTS"  -filter:v  scale=$3:-1  -an -vcodec libx264  -pass 1  -preset veryslow  -threads 0 -b 3000k -x264opts frameref=15:fast_pskip=0   -f rawvideo -y /dev/null
ffmpeg -i "$2"".MTS"  -filter:v  scale=$3:-1                       -pass 2  -preset veryslow  -threads 0 -b 3000k -x264opts frameref=15:fast_pskip=0 "$2"".ogv"
}  

old_converts()
{  
cd "/media/bill/ATA_WDC_500G/Video production/170930 DrumLibrary - kids science/Scenes/Language_translation/"
ffmpeg -i "Language translation experiment 09m51s.MTS"  -pass 1  -y  "Language translation experiment 09m51s-1.ogv"
ffmpeg -i "Language translation experiment 09m51s.MTS"  -pass 2  -y  "Language translation experiment 09m51s-2.ogv"
convert_it  "Language_translation/"    "Language translation experiment 09m51s"  1280
convert_it  "ExpandEarth_experiment/"  "ExpandEarth experiment 10m52s"            640
convert_it  "Hearing_aids/"            "Wang demo 00m46s"                         640
}  


convert_it  "Language_translation/"    "Language translation experiment 09m51s"  1280


# Lenovo to run  $ bash "/media/bill/ATA_WDC_500G/Video production/170930 DrumLibrary - kids science/video production/convert MTS to ogv.sh"

# Dell64 to run  $ bash "/home/bill/Video production/170930 DrumLibrary - kids science/video production/convert MTS to ogv.sh"



# enddoc