3

I have lots of large video files of my kids (.MTS format). Now I would like to convert them into divx / xvid to save hdd space and also have the ability to watch on my TV set.

Video files are stored in nested folders on my hdd (based on shooting times or special events). So, keeping the existing folder structure I would like to convert all the videos (which may take some days I believe) automatically.

I will appreciate if you can guide me how to achieve it?

Tolga
  • 197

2 Answers2

4

You can use ffmpeg to convert video formats. If it isn't installed yet, get it using:

sudo apt install ffmpeg

I successfully tried the following command for converting a sample video to an AVI-based DivX 5.0 format:

ffmpeg -i sample.mp4 -f avi -vtag DX50 sample.divx

First you specify the input video file as e.g. -i Videos/sample.mts, then we say we want the output format to be AVI with an DivX 5.0 tag using -f avi -vtag: DX50. The last argument is the output file to generate, here Videos/sample.divx.

I recommend you to try with one small video first and verify that everything worked as expected and both the format is recognized by your player as well as the video quality and file size is okay. You can tweak the latter with an additional argument -q:v N, where N is a number and lower numbers mean higher quality and file size. Good values are probably in the range 2-8, maybe try 5:

ffmpeg -i sample.mp4 -f avi -vtag DX50 -q:v 5 sample.divx

Then once the test was successful and both format and quality are okay, you can batch-convert all your .mts video files inside a specific source directory recursively with a Bash script like this:

#!/bin/bash

# Change the directories and quality level (lower=better) below accordingly:
inputfolder="/path/to/somewhere"
outputfolder="/path/to/elsewhere"
videoquality=5

shopt -s globstar nocaseglob nocasematch  # enable ** and globs case-insensitivity

for inputfile in "$inputfolder"/**/*.mts ; do
    outputfile="$outputfolder/$(basename "${inputfile%.mts}").divx"
    ffmpeg -i "$inputfile" -f avi -vtag DX50 -q:v "$videoquality" "$outputfile"
done

Or if you want the output files in the same directory as the input, here's a shorter version:

#!/bin/bash

# Change the directory and quality level (lower=better) below accordingly:
folder="/path/to/somewhere"
videoquality=5

shopt -s globstar nocaseglob nocasematch  # enable ** and globs case-insensitivity

for inputfile in "$folder"/**/*.mts ; do
    ffmpeg -i "$inputfile" -f avi -vtag DX50 -q:v "$videoquality" "${inputfile%.mts}.divx"
done
Byte Commander
  • 107,489
  • Thanks for your answer. I have tried the bash script but it does not run recursively inside the child folders. (my folder structure is like this: parent: /2017 child: /2017/2017\ 03/ birthday/ (yes unfortunately I have spaces inside folder names) – Tolga Mar 04 '18 at 05:11
  • The VLC video player can convert several videos. I don't know if it does what you ask, but at least using the mouse pointer and the shift key you can add several videos at once to the conversion list. – user186255 Mar 04 '18 at 10:47
  • @Tolga Can you explain what exactly didn't work? Any errors? Any files missed out? I don't think spaces should be a problem... Or did you get it to work later, as you accepted the answer? – Byte Commander Mar 04 '18 at 12:26
  • I still have issue. Problem is: output file is not placed in the same folder of the original file. Here is the example of an input file and output file=> i: /home/Public/Shared Pictures/2016/2016 11/MVI_6260.MTS o: /home/Public/Shared Pictures/2016/MVI_6260.divx – Tolga Mar 04 '18 at 13:01
  • Oh, you want them to always be in the same folder? That would simplify the script a bit, let me add another version. Maybe that one works better for you then... – Byte Commander Mar 04 '18 at 13:09
1
find -mindepth 1 -type d | xargs -n1 -I '{}' convert '{}'/* '{}'.pdf
  • find finds all files
  • -mindepth 1 ensures it doesn't include the current directory
  • -type d ensures only directories are found
  • xargs runs a command for each found directory
  • -n1 says that each line (1) will run a command
  • -I '{}' says that any time we see '{}' we replace it with the name of the directory found
  • convert is an ImageMagick tool for converting images (in this case to PDFs)
  • '{}'/* finds all the images in the directory
  • '{}'.pdf says the output PDF file is to be the name of the directory with .pdf appended