Date created: Tuesday, March 2, 2021 10:19:43 AM. Last modified: Thursday, April 25, 2024 2:34:03 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 

 

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 imagemagick-6.q16
# Provides "exiftool"
sudo apt-get install libimage-exiftool-perl

 

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

 

 

Video

Joining Files

Rename video segments to remove trailer then join them together:

$ 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

$ rename 's/\?null=0//' *

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

$ ffmpeg -i "concat:segment1_2296000_av.ts|segment2_2296000_av.ts|segment3_2296000_av.ts" -c copy -bsf:a aac_adtstoasc output.mp4

$ ulimit -n 10000 # Increase open file limit if merging hundreds of smaller chunks
$ ffmpeg -i "concat:$(ls *.mp4 | sort -V | tr "\n" "|" | head -c -1)" -c copy -bsf:a aac_adtstoasc output.mp4

 

Join split audio and video files:

# -c means copy the original codecs as-is, don't re-encode:
ffmpeg -i audiofile.m4a -i videofile.m4v -c copy combined.mp4
for file in *.m4a; do ffmpeg -i "${file}" -i "${file/m4a/m4v}" -c copy "${file/m4a/mp4}"; 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

 

Convert to GIF

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

 


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