Date created: Tuesday, March 2, 2021 10:19:43 AM. Last modified: Tuesday, November 26, 2024 5:24:21 PM

Music & Media Commands

Audio

sudo apt-get install ffmpeg

 

Splitting Files

Split flac file with cue file:

sudo apt-get install cuetools shntool flac
shnsplit -f file.cue -t %n-%t -o flac file.flac 

 

Joining FIles

Joing MP3 chunks:

ffmpeg -i "concat:$(ls *.mp3 | sort -V | tr "\n" "|" | head -c -1)" -c copy mix.mp3

 

Converting AIFF to WAV and Renaming

ffmpeg using find with bash sub-string replacement to change the filename ending:

find . -iname "*.aiff" -execdir sh -c 'x="{}"; ffmpeg -i "$x" "${x%.aiff}.wav"' \;

 

Converting Audio to FLAC

Need to specify the sample depth and sample frequency, to match the input files (in this case 16-bit 44.1Khz APE files):

for file in *.ape; do ffmpeg -i "$file" -af aformat=s16:44100 "${file/ape/flac}"; done

 

Converting Audio to MP3

Convert from flac directly to 320Kbps MP3:

# sudo apt-get install lame ffmpeg libavcodec-extra-53
for f in *.flac; do ffmpeg -i "$f" -acodec libmp3lame -ab 320k "${f%.flac}.mp3"; done

# Alternative:
find -name "*.flac" -exec ffmpeg -i {} -acodec libmp3lame -ab 320k {}.mp3

 

Decompress from flac to wav, then encode to mp3 320k

mkdir temp
flac -d *.flac
for f in *.wav; do lame -b 320k "$f" ./temp/"${f%.wav}.mp3"; done
rm *.wav 

 

Convert m4a/wma/flac audio to mp3 audio:

$ ffmpeg -i input_file.aac -acodec libmp3lame -ab 320k output_file.mp3
$ ffmpeg -i input_file.ac3 -acodec libmp3lame -ab 320k output_file.mp3
$ ffmpeg -i input_file.flac -acodec libmp3lame -ab 320k output_file.mp3
$ ffmpeg -i input_file.m4a -acodec libmp3lame -ab 320k output_file.mp3
$ ffmpeg -i input_file.ogg -acodec libmp3lame -ab 320k output_file.mp3
$ ffmpeg -i input_file.wav -acodec libmp3lame -ab 320k output_file.mp3
$ ffmpeg -i input_file.wma -acodec libmp3lame -ab 320k output_file.mp3

 

Convert video to audio:

When converting from a video file to an audio file, if the output audio file type and encoding isn't specified, the output file may be an video file, -vn tells ffmpeg to ignore the video stream so that the output is audio only:

ffmpeg -i Input_File.flv -vn -acodec copy

 

Converting Video to Audio

Convert mp4 video to mp3 (audio only):

# -i input file
# -acodec output audio codec
# -ac audio channels
# -b:a exact bitrate
# -ar audio sampling frequency
ffmpeg -i Input-Video.mp4 -acodec libmp3lame -ac 2 -b:a 320k -ar 48000 Output-Audio.mp3

# Convert all files in the current directory, using basename to replace the filename ending with mp3:
for file in *.mp4; do ffmpeg -i "$file" -acodec libmp3lame -ac 2 -b:a 320k -ar 48000 "`basename -s ".mp4" "$file"`.mp3"; done

# Instead of basename, if using BASH, us the built-in substring replacement method:
for file in *.mp4; do ffmpeg -i "$file" -acodec libmp3lame -ac 2 -b:a 320k -ar 48000 "${file/mp4/mp3}"; done

 

Pictures

# Provides "convert"
sudo apt-get install --no-install-recommends imagemagick-6.q16
# Provides "exiftool"
sudo apt-get install --no-install-recommends libimage-exiftool-perl
# Provides "rsvg-convert"
sudo apt-get install --no-install-recommends librsvg2-bin

 

Resizing Pictures

Resize by percentage:

convert infile.jpg -resize 50x50% outfile.jpg

 

Reduce quality to a certain percentage of original:

convert infile.jpg -quality 40 outfile.jpg

 

EXIF Data

Removing EXIF data:

exiftool -all= image.jpg

 

HEIC

Convert to JPG

sudo apt-get install libheif-examples
for file in *.heic; do heif-convert $file ${file/%.heic/.jpg}; done

 

PDF

Merge multiple images into a PDF

$ convert page1.png page2.png page3.png combined.pdf

 

SVG

Convert to PNG (default format for rsvg-convert)

$ rsvg-convert -o output.png -b white --dpi-x 1200 --dpi-y 1200 intput.svg

# convert doesn't seem to work as well as rsvg-convert
$ convert intput.svg -background white -density 1200 output.png

 

Convert to PDF

$ rsvg-convert -f pdf -o output.pdf -b white --dpi-x 1200 --dpi-y 1200 input.svg

 

Merge multiple SVGs into a PDF

$ rsvg-convert -f pdf -o multi-page.pdf -b white --dpi-x 1200 --dpi-y 1200 inputs_*.svg

 

Video

Renaming Files

$ ls -1
segment100_2296000_av.ts?null=0
segment101_2296000_av.ts?null=0
segment102_2296000_av.ts?null=0
segment10_2296000_av.ts?null=0

# Requires "rename"; strip "?null=0" from the end of each segment
# apt-get install rename
$ rename 's/\?null=0//' *

# Pure BASH; strip "?null=0" from the end of each segment
$ for file in *ts?*; do mv "${file}" "${file/?null=0/}"; done

$ ls -1
segment100_2296000_av.ts
segment101_2296000_av.ts
segment102_2296000_av.ts
segment10_2296000_av.ts

 

Joining Files

Join two videos together without re-encoding:

ffmpeg -i "concat:file1.avi|file2.avi" -c copy "/tmp/joined.avi"

 

Join multiple files together which contain both audio and video:

# Specify the list of files to concat in the command, they must be pipe-separated with no white space
$ ffmpeg -i "concat:segment1_2296000_av.ts|segment2_2296000_av.ts|segment3_2296000_av.ts" -c copy -bsf:a aac_adtstoasc output.mp4

# Build the concat list at run time
$ ulimit -n 10000 # Increase open file limit if merging hundreds of smaller chunks
# Combined audio and video mp4 chunks
$ ffmpeg -i "concat:$(ls *.mp4 | sort -V | tr "\n" "|" | head -c -1)" -c copy -bsf:a aac_adtstoasc output.mp4
# Separate audio and video ts chunks
$ ffmpeg -i "concat:$(ls *a1.ts | sort -V | tr "\n" "|" | head -c -1)" -i "concat:$(ls *v1.ts | sort -V | tr "\n" "|" | head -c -1)" -c copy -bsf:a aac_adtstoasc output.mp4

 

Join split audio and video files:

# Join a single audio and video file
# -c means copy the original codecs as-is, don't re-encode:
ffmpeg -i audiofile.m4a -i videofile.m4v -c copy combined.mp4

# Join multiple m4a audio and m4v video files with the same filename, into multiple combined mp4 files, which can then later be combined into a single mp4:
for file in *.m4a; do ffmpeg -i "${file}" -i "${file/m4a/m4v}" -c copy "${file/m4a/mp4}"; done

# Join multiple audio only ts files with multiple video only ts files, into multiple combined ts files, which can then later be combined into a single mp4
for file in *a1.ts; do ffmpeg -i "${file}" -i "${file/a1/v1}" -c copy "${file/-a1.ts/.ts}"; done

 

Remove the audio from a video:

ffmpeg -i $1 -c copy -an "$1-nosound.${1#*.}"

 

Downscale a video:

#Scale by input width / 2 and input height / 2 to keep the same aspect ratio
ffmpeg -i input.avi -vf scale="iw/2:ih/2" output.avi

 

Converting videos

Convert a video file to a gif:

"Control looping with -loop output option but the values are confusing. A value of 0 is infinite looping, -1 is no looping, and 1 will loop once meaning it will play twice. So a value of 10 will cause the GIF to play 11 times."

ffmpeg -i input.mp4 -vf "fps=20,scale=320:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" -loop -1 output.gif

 

Convert mkv to mp4:

Convert the container from mkv to mp4, but copy the video and copy like-for-like:

ffmpeg -i input.mkv -c:v copy -c:a copy output.mp4

 

Copy the video but re-encode the audio:

ffmpeg -i input.mkv -c:v copy -c:a libvorbis output.mp4

 


Previous page: 'mdadm' - Notes
Next page: 'perf' - Notes