A few very simple Video file manipulation tips for Linux users

This post is obviously aimed at Linux users, as it is my prefred desktop of choice.

Shrinking the size of Video files (linux users only)

Sometimes Video files you have, whether provided by a friend, downloaded from youtube, or even from your own video camera cam be huge. It is not uncommon to see MP4 Video files over 1Gb in size for 30mins of footage while having other MP4 files of high quality footage of the same length using less than 300Mb of space.

The human eye can only see a certain number of video frames a second and many high end video capture devices record a lot more than needed, often at a higher quality than needed as well.

The following command will shrink a 1Gb MP4 file to around 300Mb with minimal loss of quality. But be sure to check the quality of the new file meets you needs before deleting the original as depending on the source file there can be quality loss.

ffmpeg -i input.mp4 -vcodec libx265 -crf 24 output.mp4

It is however important to note that ffmpeg will take a long time to run, for a 30min Video file allow 30mins, and if you are trying to shrink a small file like a 250Mb file the output may be bigger than the original file so only use to shrink huge files.

Also ffmpeg has a lot more options that can be used to control output you should look at, the above is a generic command but there are many other options such as frames per second that could shrink a file even further; but that would be on a individual file bases after checking existing file settings with ffprobe to decide if you want to play with them.

Removing DRM from Video files

This is not about removing DRM from media such as DVDs, but for Video files such as MP4 files you may have.

Sometimes your friends may helpfully record something off TV for you through a subscription service, or you are provided with a video file from a download service you subscribe to that is DRM protected and even though you purchased the download it can only be viewed while connected to that service. This is not about content protection but about inflating their service numbers, for example they can tell their shareholders they had nn connected users on a given date but omit that 90% of them were just re-watching something downloaded months earlier.

In many countries it is illegal to remove DRM; although in most if you have purchased or legally obtained the content it is acceptable to do so for your own use. If you fall into the later case read on.

If you search the internet there are many paid products you can purchase to remove DRM, they should be avoided as they are obviously trying to make money from a dubious legal position, and there are many free alternatives plus you probably already have one installed.

I am referring of course to the VLC media player that most Linux and Windows users already have installed and use in preference to anything bundled with the OS as it is simply better.

As well as VLC helpfully playing DVDs you may have purchased online for out-of-country zones you will have noticed it will also in many cases play DRM protected Video files. As VLC is my preferred player I was astounded one day when a file I was watching via VLC I decided to finish watching in bed (by putting the file on a usb stick) but my JVC TV told me it would not play the file because the file was DRM protected. Astounded ?, actually pissed off because I was halfway through watching it.

Anyway the key thing here is that the file must be playable using VLC. I am sure all VLC users know there is a “record” option in VLC which could do the trick if you are happy to watch the Video file, but most users of VLC will probably never use the streaming functions that VLC provides and not know that one of the stream outputs can be a file (and that video to the screen can be disabled) allowing VLC to batch play/stream-to-file an input Video file it is able to play with the output going to a file… in considerably less time than it would take to watch the Video file.

The below command is for Linux users, but similar syntax can be used for Windoze users.

vlc -I dummy "someinputfile.mp4" \
 --sout="#transcode{vcodec=mpeg4,vb=1024,acodec=mp3,ab=192,channels=2,deinterlace}:std{access=file,mux=ts,dst=someoutputfile.mp4" \
 vlc://quit

There will sometimes be quality loss (observed ‘fuzziness’ in display and occasional freezing on the new output file in a few cases) but in most cases no human observable difference in playback between the two files. You may have to tweak the parameters although I have found the ones above work in most cases.

Extracting audio only

Some video files (such as some audio books) come as MP4 files with a slideshow or static picture for the length of what should just be an audio file.

If like me you consider this a waste of disk space, it is simple to extract the audio on linux servers using the below command (note you may use aac output instead of m4a in this command is preferred).

ffmpeg -i input.mp4 -vn -c:a copy output.m4a

If you wish to use MP3 output instead note that MP3 files are a completely different format so it is not possible to simply copy the input stream to copy the audio, it has to be re-compressed and will in most cases result in a larger file. The command to extract the audio only from a MP4 video file to a MP3 audio file would be

ffmpeg -i input.mp4 -vn -ab 250k output.mp3

And for non-Linux users (or even Linux users) you can of course use VLC. The media -> convert/save dialog options can walk you through saving only the audio stream from a video file as a MP3 file.

Concatenating Video files

Occasionally you may download files from YouTube that came as part-n-of-n files and are a pain to watch as individual pieces. It would be nice to concatenate them into a single file.

For files with identical attributes they can simply be concatenated with video editing tools such as AviDemux; unfortunately while working fine for personally recorded videos with common attributes that and many other video tools will not be able to concatenate files if they have majorly different attributes.

Yet again ffmpeg can come to the rescue. Given a list of video files provided in a file, it can concatenate all the files together even if they have vastly different properties. It is important to note the the filenames in the list should not have spaces in the filename. A quick example…

cp '/some/dir/SS Eat the Fifties Part 1.mp4' input1.mp4
cp '/some/dir/SS Eat the Fifties Part 2.mp4' input2.mp4
cp '/some/dir/SS Eat the Fifties Part 3.mp4' input3.mp4
cp '/some/dir/SS Eat the Fifties Part 4.mp4' input4.mp4
cp '/some/dir/SS Eat the Fifties Part 5.mp4' input5.mp4
cp '/some/dir/SS Eat the Fifties Part 6.mp4' input6.mp4
cat << EOF > filelist.txt
file input1.mp4
file input2.mp4
file input3.mp4
file input4.mp4
file input5.mp4
file input6.mp4
EOF
ffmpeg -f concat -i filelist.txt -c copy Supersizes_Eat_The_Fifties.mp4

For the older format (flv) videos you may have collected will often refuse
to me merged as ffmpeg has dropped FLV from that functionality now. For FLV files you now have to convert them to MP4 first. I use this script where the parameter is simply a string that will find them all by wildcard name…

#!/bin/bash
#
# This script is intended to merge all the 1-of-N files into 1-of-1 merged files.
# MP4 files can be merged as is in most cases
# FLV files have to be converted, then merged
# AVI I am not sure of yet... still doing all the flv ones
#
# WARNING: All mine are n-of-5 and that is used in the 'sed' near
#          the end of the script. You may want to change that
#
# Syntax example : ./merge_files.sh *Balsamic*
#
# DO NOT DELETE THE ORIGINALS UNTIL YOU HAVE TESTED THE MERGED FILES
#

# Merge misc files into one, they should all be exactly the same input
# framerate, video hight/width, audio rate etc or we have to fic that
mask="$1"

# empty eny list left over from a previous run
> filelist.txt

# FLV cannot be merged, if FLV files are provided re-encode each file
# to MP4 first
intype=`ls ${mask}* | head -1 | awk -F\. {'print $2'}`
if [ "${intype}." == "flv." ];
then
   ls ${mask}* | while read xx
   do
      yy=`echo "${xx}" | sed -e's/.flv/.mp4/g'`
      echo "ffmpeg -i ${xx} -vcodec libx264 -acodec copy ${yy}" >> cmdxx.sh
   done
   if [ -f cmdxx.sh ];
   then
      bash cmdxx.sh
      cat cmdxx.sh
      /bin/rm cmdxx.sh
   fi
   ls ${mask}*mp4 | while read xx
   do
      echo "file ${xx}" >> filelist.txt
   done
else
   # else should be mp4 files so should merge fine
   ls ${mask}* | while read xx
   do
      echo "file ${xx}" >> filelist.txt
   done
fi
# merge all the files in the filelist.txt now, append into name ffmpegmerged
# as I do not want to overwrite any origionals until I have tested the
# output file.
outname=`ls ${mask}* | head -1 | sed -e's/1-of-5/1-of-1/g' \
   | awk -F\. {'print $1"-ffmpegmerged.mp4"'}`
ffmpeg -f concat -i filelist.txt -c copy ${outname}
exit 0

Note: I prefer guayadeque for playing music and audio files, it is in the Fedora repositories; and VLC for video files which Fedora/CentOS users should get from rpmfusion.

About mark

At work, been working on Tandems for around 30yrs (programming + sysadmin), plus AIX and Solaris sysadmin also thrown in during the last 20yrs; also about 5yrs on MVS (mainly operations and automation but also smp/e work). At home I have been using linux for decades. Programming background is commercially in TAL/COBOL/SCOBOL/C(Tandem); 370 assembler(MVS); C, perl and shell scripting in *nix; and Microsoft Macro Assembler(windows).
This entry was posted in Unix. Bookmark the permalink.