Split an mp3 into parts with Bash
Introduction
Before reading this article, make sure you are familiar with the content of the article «Bash basics»
You can cut a piece out of an .mp3 file with the command
ffmpeg -i source.mp3 -vn -acodec copy -ss H:i:s -to H:i:s output.mp3
An example of cutting the first twenty minutes from the file source.mp3 into the file output.mp3
ffmpeg -i source.mp3 -vn -acodec copy -ss 00:00:00 -to 00:20:00 output.mp3
ffmpeg
Split the file audiobook.mp3 into half-hour parts.
#!/bin/bash SOURCE=audiobook for i in `seq 0 9`; do let k=$i+1; let p=$i*2; if [[ "$p" -lt 10 ]]; then z=0; else z=""; fi let l=$p+1; if [[ "$l" -lt 10 ]]; then t=0; else t=""; fi echo $p; echo "------------" echo $l; ffmpeg -i $SOURCE.mp3 -vn -acodec copy -ss 0$i:00:00 -to 0$i:30:00 $SOURCE.part_$z$p.mp3 ffmpeg -i $SOURCE.mp3 -vn -acodec copy -ss 0$i:30:00 -to 0$k:00:00 $SOURCE.part_$t$l.mp3 done
#!/bin/bash SOURCE=HP_7 # ffprobe -i $SOURCE.mp3 -show_entries format=duration -v quiet -of csv="p=0" # sexagestimal for i in `seq 0 3`; do let k=$i+1; let p=$i*2; if [[ "$i" -lt 10 ]]; then y=0; else y=""; fi if [[ "$k" -lt 10 ]]; then x=0; else x=""; fi if [[ "$p" -lt 10 ]]; then z=0; else z=""; fi let l=$p+1; if [[ "$l" -lt 10 ]]; then t=0; else t=""; fi echo $p; echo "------------" echo $l; ffmpeg -i $SOURCE.mp3 -vn -acodec copy -ss $y$i:00:00 -to $y$i:30:00 $SOURCE.part_$z$p.mp3 ffmpeg -i $SOURCE.mp3 -vn -acodec copy -ss $y$i:30:00 -to $x$k:00:00 $SOURCE.part_$t$l.mp3 done
Article author: Andrei Olegovich