Date created: 03/02/21 10:19:43. Last modified: 03/03/21 10:56:15

Music & Media Commands

Split flac file with cue file:

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


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


Joining Files

Rename video segments to remove trailer then join them together:

$ ls -1

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

$ ls -1

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


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