Skip to content

Instantly share code, notes, and snippets.

@glaszig
Created June 29, 2022 04:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glaszig/c90bf5463bf3b6e82846c56448ea2e2a to your computer and use it in GitHub Desktop.
Save glaszig/c90bf5463bf3b6e82846c56448ea2e2a to your computer and use it in GitHub Desktop.
generate m3u from directory
#!/usr/bin/env sh
# generates extended m3u for all media files in a directory.
#
# requirements: ffprobe (ffmpeg)
# usage: $0 path/to/media/files [playlist.m3u]
# author: mail@glasz.org
# license: wtfpl - http://www.wtfpl.net/txt/copying/
set -e
if [ ! -d "$1" ]; then
echo "first argument must be a directory"
echo "usage: $0 directory [index.m3u]"
exit 1
fi
path="$1"
name=${2:-"index.m3u"}
m3u="$path/$name"
dotm3u="$path/.$name"
echo "#EXTM3U" > "$dotm3u"
echo "" >> "$dotm3u"
ls "$path" | while IFS= read -r f; do
if [ "$name" == "$f" ]; then continue; fi
i=$(ffprobe -v 0 -of json -show_format "$path/$f" | jq '.format')
a=$(echo "$i" | jq -r '.tags.artist')
t=$(echo "$i" | jq -r '.tags.title')
l=$(echo "$i" | jq -r '.duration | tonumber | ceil')
echo "#EXTINF:$l,$a - $t" >> "$dotm3u"
echo "$f" >> "$dotm3u"
echo "" >> "$dotm3u"
done
rm -f "$m3u" && mv "$dotm3u" "$m3u"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment