-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopus_to_mobile.sh
More file actions
executable file
·63 lines (52 loc) · 1.46 KB
/
opus_to_mobile.sh
File metadata and controls
executable file
·63 lines (52 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#! /bin/bash
# given directories containing media files, transcode them as Opus audio and save them directly to an MTP device
# assumes MTP device is mounted via GVFS by the current user (standard in Ubuntu)
# also copies album art if present
ffmpeg_args='-b:a 64k -ac 2 -n -nostdin' # no video, 64 kbps, force stereo, do not overwrite nor ask about it, non-interactive
devicepath=/run/user/$UID/gvfs/mtp*
outpath='Internal shared storage/Music'
filetypes=(mp3 flac m4a mp4)
artfiles=(Folder.jpg AlbumArt*.jpg)
if [ ! -n "$1" ]
then
echo "usage: $(basename $0) directory1 directory2 ..."
exit 1
fi
if [ $(ls -d $devicepath | wc -l) -eq 0 ]
then
echo 'error: no MTP device found'
exit 1
elif [ $(ls -d $devicepath | wc -l) -gt 1 ]
then
echo 'error: multiple MTP devices connected'
exit 1
fi
devicepath=$(echo $devicepath)
wd=$(pwd)
set -euo pipefail
for dir in "$@"
do
dirname=$(basename "$dir")
if [ ! -d "$devicepath/$outpath/$dirname" ]
then
mkdir "$devicepath/$outpath/$dirname"
fi
for filetype in ${filetypes[@]}
do
if [ $(compgen -G "$dir/*.$filetype" | wc -l) -gt 0 ]
then
for sourcefile in "$dir"/*.$filetype
do
newfile=$devicepath/$outpath/$dirname/$(basename "$sourcefile" | sed "s/\.$filetype$/.opus/")
ffmpeg -i "$sourcefile" $ffmpeg_args "$newfile"
done
fi
done
for arttype in ${artfiles[@]}
do
if [ $(compgen -G "$dir/$arttype" | wc -l) -gt 0 ]
then
cp "$dir"/$arttype "$devicepath/$outpath/$dirname/"
fi
done
done