Importing AVCHD Video From A Sony HD-XR100 Camera

From Nearline Storage
Jump to: navigation, search
  1. Plug camera in and start it up as a USB disk
  2. Copy the *.mts files from /media/disk/avchd/bdmv/stream directory into a new directory on disk
  3. Disconnect the camera
  4. Run m2tsconv *.mts in the new directory to convert all of the *.mts files to DVD-ready format (~8 hours per hour of video on my workstation)
    • m2tsconv -a 16:9 -a 4:3 *.mts will build both widescreen and letterboxed versions of the videos in a single pass.
  5. Use avidemux2_gtk, kino or kdenlive to make any edits necessary
    • Avidemux, kino and kdenlive can combine multiple MPG files into one for the DVD. This is desirable because otherwise there are jerky breaks in the video where one file ends and another one starts. All of the MPGs that make up one title on a DVD should be combined into one MPG. If you need transitions, then use kino or kdenlive to do the editing instead.
  6. kdenlive will read *.MTS files directly now so use it to combine and edit the clips in your video.
  7. Use DVDStyler to build a simple menu and to author a DVD iso.
    • The MPGs from m2tsconv, Avidemux, kdenlive or Kino are already DVD-ready, you can tell DVDStyler this in the properties of each MPG. This eliminates the need to recode the file and saves a bunch of time.
    • DVDStyler is better than DeVeDe because it can build both 16:9 and 4:3 menus while DeVeDe only does 4:3.
  8. Burn DVD iso onto DVD with cdrecord: cdrecord -v dev=/dev/scd0 file.iso
    • To get a list of available devices: cdrecord --devices

Making Flash videos

I have hacked the self-contained movie player out of Camtasia so that I can plug any movie into it. See ~/Videos/Flash Framework for that plus the mkmovie script mentioned below:

  1. Generate 16:9 mpg(s) using m2tsconv as described above
  2. If you need transitions, edit the video using Kino or kdenlive
    • Export final output as an MPEG in DVD format
    • Use 4:3 as the output aspect, this will generate a letterboxed MPEG which you can crop with mkmovie (edit script and see option strings at the top.) Crop top and bottom both by 60. (Check these crop values with "mplayer -vf cropdetect" if you want to.
  3. Convert the output MPEG to a MP4 Flash movie file using the mkmovie script
  4. Edit the movie_config.xml to set the description and run time for the movie

m2tsconv

This script depends on:

  1. ffmpeg from your Linux distribution
  2. JM H.264/AVC reference tools - download from http://iphome.hhi.de/suehring/tml/ and compile
  3. xport Transport Stream Demuxer - download from http://w6rz.net/xport.zip and compile

It's based on work by Axel Olmos -> http://www.avsforum.com/avs-vb/showthread.php?t=789775

#!/bin/sh

#  Process M2TS files that match the input glob, or that are sitting
#  on my HandyCam's disk, into standard formats:
#
#  1920x1080 MPEG4/AC3 AVI file (16:9)
#  1920x1440 MPEG4/AC3 AVI file (Letterboxed 4:3)
#  720x480 DVD-ready MPG file (16:9)
#  720x480 DVD-ready MPG file (Letterboxed 4:3)

#  Who am I?
ME=`basename $0`

#  Parse the command line options
ASPECTS=""
TYPES=""
OPTS=`getopt -o a:t: --long aspect:,type: -n $ME -- "$@"`
if [ $? != 0 ]; then
  echo "Usage: $ME [-a X:Y|--aspect X:Y] [-t type|--type=type] m2ts-file"
  echo "   Where valid types are \"avi\" or \"dvd\" and X:Y is either \"16:9\" or \"4:3\""
  echo "   Multiple -a and -t options can be specified to produce multiple output files"
  echo "   in one run.  Default type is dvd.  Default aspect ratio is 16:9.  If a"
  echo "   target output file already exists, processing for it will be skipped."
  exit 1
fi
eval set -- "$OPTS"
while true; do
  case "$1" in
    -a|--aspect) NEWASPECT="$2"
               case "$NEWASPECT" in
                 "16:9" | "4:3" ) ASPECTS="$ASPECTS $NEWASPECT" ;;
                 *) echo "The \"$NEWASPECT\" aspect ratio is not supported"; exit ;;
               esac
               shift 2 ;;
    -t|--type) NEWTYPE=`echo "$2" | tr [:upper:] [:lower:]`
               case "$NEWTYPE" in
                 "avi" | "dvd" ) TYPES="$TYPES $NEWTYPE" ;;
                 *) echo "Conversion to \"$NEWTYPE\" is not supported"; exit ;;
               esac
               shift 2 ;;
    --) shift; break ;;
  esac
done
if [ -z "$ASPECTS" ]; then
  ASPECTS="16:9"
fi
if [ -z "$TYPES" ]; then
  TYPES="dvd"
fi

if [ "$*" = "" ]; then
  WHERE="/media/disk/avchd/bdmv/stream/*.mts"     #  Linux mounts my camera's disk here
else
  WHERE="$*"
fi

# Make a directory for temporary files
CTR=1
TMPDIR="/tmp/${ME}_"$RANDOM
while ! mkdir -m 755 "$TMPDIR"; do
  if [ $CTR -gt 10 ]; then
    echo "Unable to create directory for temporary files in $CTR tries: $TMPDIR"
    exit 1
  fi
  CTR=$(( $CTR + 1 ))
  TMPDIR="/tmp/${ME}_"$RANDOM
done

AUDIOFILE="${TMPDIR}/audio.ac3"
VIDEOFILE="${TMPDIR}/video.yuv"
PASSLOG="${TMPDIR}/PASSLOG"

STARTSECS=`date +"%s"`

for FILE in $WHERE ; do

  for TYPE in $TYPES ; do

    for ASPECT in $ASPECTS ; do

      OUTNAME=`basename $FILE`
      case "$TYPE" in
        "avi") case "$ASPECT" in
                 "4:3") OUTNAME="${OUTNAME%.*}.letterboxed.avi" ;;
                 *) OUTNAME="${OUTNAME%.*}.avi" ;;
               esac
               ;;
        *) case "$ASPECT" in
             "4:3") OUTNAME="${OUTNAME%.*}.letterboxed.mpg" ;;
             *) OUTNAME="${OUTNAME%.*}.mpg" ;;
           esac
           ;;
      esac

      if [ -e "$OUTNAME" ]; then
        echo "$OUTNAME already exists, skipping ..."
      else

        if [ ! -e "$AUDIOFILE" ]; then
          xport -h "$FILE" 1 1 1 && mv bits0001.mpa "$AUDIOFILE"
        fi
        if [ ! -e "$VIDEOFILE" ]; then
          ldecod -i bits0001.mpv -o "$VIDEOFILE"
        fi

        for (( PASS=1; PASS <= 2; PASS++ )); do

          case "$TYPE" in
            "avi") PASS=2; PASS_OPTS="" ;;
            *) PASS_OPTS="-pass $PASS -passlogfile $PASSLOG" ;;
          esac

          if [ $PASS -eq 1 ]; then
            OUTFILE="/dev/null"
            A_ENCODE="-an"
          else
            OUTFILE="$OUTNAME"
            case "$TYPE" in
              "avi") A_ENCODE="-acodec copy" ;;
              *) A_ENCODE="" ;;
            esac
          fi

          case "$TYPE" in
            "avi") V_ENCODE="-vcodec mpeg4 -sameq -b 15000k" ;;
            *) V_ENCODE="-target ntsc-dvd" ;;
          esac
          case "$ASPECT" in
            "4:3") case "$TYPE" in
                     "avi") V_ENCODE="$V_ENCODE -aspect 4:3 -padtop 180 -padbottom 180 -s 1920x1080" ;;
                     *) V_ENCODE="$V_ENCODE -aspect 4:3 -padtop 58 -padbottom 58 -s 720x364" ;;
                   esac
                   ;;
            *) V_ENCODE="$V_ENCODE -aspect 16:9" ;;
          esac

          ffmpeg -y -r 29.97 -s 1920x1080 -i "$VIDEOFILE" -i "$AUDIOFILE" $V_ENCODE $A_ENCODE $PASS_OPTS "$OUTFILE"

        done    # pass

        echo "$OUTFILE is ready"
        rm "$PASSLOG" &>/dev/null

      fi

    done    # aspect

  done    # type

  rm -f $TMPDIR/* dataDec.txt log.dec bits0001.mpv

done    # file

rm -fr $TMPDIR

ENDSECS=`date +"%s"`
HOURS=$(( ( $ENDSECS - $STARTSECS ) / 3600 ))
MINUTES=$(( ( $ENDSECS - $STARTSECS - ( $HOURS * 3600 ) ) / 60 ))
SECONDS=$(( ( $ENDSECS - $STARTSECS - ( $HOURS * 3600 ) - ( $MINUTES * 60 ) ) ))
if [ $SECONDS -eq 1 ]; then
  TIME_STR="$SECONDS second"
else
  TIME_STR="$SECONDS seconds"
fi
if [ $MINUTES -gt 0 ]; then
  if [ $MINUTES -eq 1 ]; then
    TIME_STR="$MINUTES minute, $TIME_STR"
  else
    TIME_STR="$MINUTES minutes, $TIME_STR"
  fi
fi
if [ $HOURS -gt 0 ]; then
  if [ $HOURS -eq 1 ]; then
    TIME_STR="$HOURS hour, $TIME_STR"
  else
    TIME_STR="$HOURS hours, $TIME_STR"
  fi
fi
echo "That all took $TIME_STR"

any2avi

This script depends on:

  1. ffmpeg from your Linux distribution
#!/bin/sh

# Make MS Media Player compatible AVI file

two_pass=true
passlogfile="/tmp/any2avi.log"

# For 4:3
# video_opts="-aspect 4:3 -s 480x360 -vcodec msmpeg4v2 -b 1M -bt 2M -f avi"

# For 16:9
video_opts="-aspect 16:9 -s 640x360 -vcodec msmpeg4v2 -b 1M -bt 2M -f avi"

# Crop 4:3 letterbox to 16:9
video_opts="-croptop 60 -cropbottom 60 $video_opts"

audio_opts="-acodec libmp3lame -ac 2"

#  Host-specific options
case "${HOSTNAME%%.*}" in
  mythtv)
    output_path="/storage/"
    ;;
  fang)
    output_path="/home/dlk/"
    ;;
  *)
    echo "Unknown host, please edit script to set output_path for this host"
    exit
    ;;
esac

#  Get input filename
input=$*
while [ -z "$input" ]; do
  read -p "Enter name of input file: " input
  if [ ! -e "$input" ]; then
    echo "Cannot find $input"
    unset input
  fi
done

for file in "$input"; do

  rm "$passlogfile" &>/dev/null

  # Encode
  if $two_pass; then
    ffmpeg -y -pass 1 -passlogfile "$passlogfile" -i "$file" \
      $video_opts -an /dev/null
    pass="-pass 2 -passlogfile $passlogfile"
  else
    pass=""
  fi

  title=`basename "$input"`
  ffmpeg $pass -i "$file" $video_opts $audio_opts "${output_path}${title%.*}.avi"

  rm "$passlogfile" &>/dev/null

done