⬆️ ⬇️

The script automatically convert m3u to m3u8

Relatively recently moved to GNU / Linux. Transforming playlists from foobar2000 to .m3u, I noticed that there are still .m3u8. It turns out that this is the same .m3u, but in UTF-8 encoding. Disorder, I thought, and decided to make everything look kosher, i.e. Translate .m3u lists to .m3u8, because All files are stored in UTF-8. I have quite a lot of these playlists (over 100), therefore I decided to write a script on the bash.



Since the music was copied from the NTFS partition to the ext4 partition - the encoding of all the files (playlists) was CP1251, and the rights of all the files included the “executable” bit — I also decided to fix it.



So, what does the written script do:



A “silent” and detailed execution mode has been implemented, accurate list conversion is provided (the old ones will be deleted only if the conversion is successful); The bug of naming Russian files was detected and fixed (my Debian Squeeze uses dash as sh, he blunt - bash does exactly the right thing).



The script listing below should be saved as “m3migr” and made executable (“chmod + x m3migr”). Starting - "./m3migr <music_dir>" - silent mode, or "./m3migr -v <music_dir>" - will output all actions to the console.

')

Playlists obtained after conversion were tested on Amarok 2.3.0 - everything works.



Actually, the script itself:




#!/bin/bash # NOTE:   /bin/sh -     #       Windows #  .m3u  .m3u8    #     # ---------------------------------  ------------------------------------ #   m3u   m3u8 (UTF-8) convert_m3u() { old_filename="$1" # .m3u new_filename="${1}8" # .m3u8 if [ "$is_verbose" = "true" ]; then printf "convert \"${old_filename}\" to \"${new_filename}\"\\n" fi iconv -f CP1251 -t UTF-8 "$old_filename" > "$new_filename" ls_tmp=$(ls -s "$new_filename") new_file_size=${ls_tmp%% *} if [ -f "$new_filename" ] && [ -n "$new_file_size" ] && [ "$new_file_size" -gt 0 ]; then rm -f "$old_filename" rm -f "${old_filename}~" rm -f "${new_filename}~" fi } #      chmod_file() { if [ -f "$1" ]; then #   if [ "$is_verbose" = "true" ]; then printf "change mode for file \"${1}\"\\n" fi chmod 644 "$1" # - rw- r-- r-- fi } #      chmod_dir() { if [ -d "$1" ]; then #   if [ "$is_verbose" = "true" ]; then printf "change mode for dir \"${1}\"\\n" fi chmod 755 "$1" # d rwx- rx rx fi } # ----------------------------   ---------------------------------- is_verbose="false" #    if [ "$1" = "-v" ] || [ "$1" = "--verbose" ]; then is_verbose="true" shift fi if [ -d "$1" ]; then music_dir="$1" else printf "usage: m3migr [{-v | --verbose}] music_dir\\n" exit 1 fi printf " ...\\n" sleep 1 printf "   ...\\n" cur_file="" find "$1" -name "*" -type f -print | while read cur_file do chmod_file "$cur_file" done sleep 1 printf "   ...\\n" cur_dir="" find "$1" -name "*" -type d -print | while read cur_dir do chmod_dir "$cur_dir" done sleep 1 printf "  m3u   m3u8...\\n" cur_file="" find "$1" -name "*.m3u" -type f -print | while read cur_file do convert_m3u "$cur_file" done printf "\\nOK!\\n" exit 0 



References



1. Neil Matthew, Richard Stones. Basics of Linux programming, 4th edition. Chapter 2

2. Summary of Bash on LOR

3. Habrahabr. Basics of bash part 1

4. Habrahabr. Basics of bash part 2

Source: https://habr.com/ru/post/95210/



All Articles