Posts Tagged ‘split’

Split a long mp3 audio file into 3 min files

Wednesday, July 29th, 2015

After some frustration trying to get mp3splt to work, I caved in and wrote a script to split apart a large audio file into many 3min chunks. Save this in mp3split.sh:

#!/bin/bash
big="$1"
duration_stamp=$(ffmpeg -i "$big" 2>&1 | grep Duration | sed 's/^.*Duration: *\([^ ,]*\),.*/\1/g')
title=$(ffmpeg -i "$big" 2>&1  | grep "title *:" | sed 's/^.*title *: *\(.*\)/\1/g')
# get minutes as a raw integer number (rounded up)
prefix=$(basename "$big" .mp3)
echo $duration_stamp
mins=$(echo "$duration_stamp" | sed 's/\([0-9]*\):\([0-9]*\):\([0-9]*\)\.\([0-9]*\)/\1*60+\2+\3\/60+\4\/60\/100/g' | bc -l | python -c "import math; print int(math.ceil(float(raw_input())))")
ss="0"
count="1"
total_count=$(echo "$mins/3+1" | bc)
while [ "$ss" -lt "$mins" ]
do
  zcount=$(printf "%05d" $count)
  ss_hours=$(echo "$ss/60" | bc)
  ss_mins=$(echo "$ss%60" | bc)
  ss_stamp=$(printf "%02d:%02d:00" $ss_hours $ss_mins)
  ffmpeg -i "$big" -acodec copy -t 00:03:00 -ss $ss_stamp -metadata track="$count/$total_count" -metadata title="$title $zcount" "$prefix-$zcount.mp3" 
  ss=$[$ss+3]
  count=$[$count+1]
done

The execute mp3split.sh my-long-file.mp3. This will output a sequence of files:

my-long-file-00001.mp3
my-long-file-00002.mp3
my-long-file-00003.mp3
my-long-file-00004.mp3
...

Each will retain the meta data from the original file except the file number will be appended to the track name and the track number will be set accordingly (i.e. this will work well for splitting enormous audiobook files into file lists that play in the correct sequence on an iphone).

Note: mp3splt really seems like the right tool for this. It supposedly has fancy features like silence detection and presumably won’t reload the file for each new split.

Trim video with VLC on mac

Sunday, September 4th, 2011

Here’s a snippet to use VLC.app to trim a movie. Times are given in seconds.


/Applications/VLC.app/Contents/MacOS/VLC --start-time 170 --stop-time 310 --sout=#file{dst=out.mkv} --no-sout-rtp-sap --no-sout-standard-sap --sout-keep  "in.mkv"

For some reason this is very quick but doesn’t exit on its own.

Split (large) files in half using bash script

Monday, June 21st, 2010

Here’s a simple bash script that splits a given file into two equal (up to a byte) halves. It uses split which has different syntax between the GNU and BSD implementations, so see the comments for switching between the two.
I tested this on a 6.4 GB dmg file and I was able to reverse the split with cat just fine. I diffed the re-concatenated file and the original and they were byte-wise equal.

Save this in a file called half.sh:


#!/bin/bash
#
# usage: half foo 
#
# This will create foo.aa and foo.ab, where foo.aa is the first half(+1) of foo
# and foo.ab is the second half. Split by bytes not lines.
#
#
# To reverse just issue:
# cat foo.aa foo.ab > foo
#

# get file size
# GNU:
#size=`stat -c "%s" "$1"`
# BSD:
size=`stat -f "%z" "$1"`

# divide by 2 and round up
half_size=`echo "($size+1)/2" | bc`
split -b $half_size "$1" "$1."