It is currently Mar 24th, '19, 19:01

Topic: From Scripts to Packages: recontainer-media.sh

Development forum
Various experiments, side projects and documentation

From Scripts to Packages: recontainer-media.sh

Unread postby Konomi » Nov 3rd, '14, 02:02

So basically anything I am working on (and I mostly script but I have aspirations for using lower level languages) generally starts it's life in ~/Scripts/somename.sh or if I am feeling really lazy ~/test.sh (Equips suit of rage resistance). Anyway I cooked this up last night, it is by no means to be considered safe or official! You have been warnnnneedddddded.

Anyway I wanted to convert a bunch of troublesome m4a files that end up with the fourcc of dash instead of say mpeg-4, this confuses a lot of programs that can actually work on the aac audio inside the m4a container but have never heard of mpeg-dash. Now if I spend enough time on this script eventually it might become an unofficial package fore Debian! (Unofficial means in my little collection of packages but I may soon be making those public) So the code:

Code: Select all
#!/usr/bin/env bash

# ffmpeg explodes the file if you use the extension .aac
# makes malformed files never use that file extension for
# file_extension_out



file_extension_in=".m4a"
file_extension_out=".m4a"



nano_secs() {
   ns="$(date +'%N')"
   if [ -z "$ns" ]; then
      echo "Nanoseconds null, error"
      exit "1"
   fi
}; nano_secs



# need our list of files probably should handle shopts better though
file_list() {
   shopt -s globstar nullglob
   file_list=(./**/*$file_extension_in)
   shopt -u globstar nullglob
}; file_list



# some asking of the user aka me cause I make mistakes
for i in "${file_list[@]}"; do
   echo "$i"
done
read -p "Process these files? [y/n] " choice
if [ "${choice,,}" != "y" ]; then
   echo "Cancelled"
   exit "0"
fi



# using a main function has the advantage of being able to return
# out of the function at any time instead of using exit to finish
# the script
main() {
   for i in "${file_list[@]}"; do
      # move i to file_in
      file_in="$i"
      # generate a file name temporarly to move the file_in to
      # a new file name
      file_mv="${file_in%$file_extension_in}.$ns$file_extension_in"
      # move the file
      mv "$file_in" "$file_mv"
      # generate the file out name
      file_out="${file_in%$file_extension_in}$file_extension_out"
      # process with ffmpeg
      ffmpeg -i "$file_mv" -vcodec copy -acodec copy "$file_out"
      ffmpeg_exit_code="$?"
      # remove the file on successful conversion
      if [ -e "$file_out" ] && [ "$ffmpeg_exit_code" == "0" ]; then
         # remove the original file
         rm "$file_mv"
      fi
   done
}; main "$@"
1. Follow the order of operations
2. Don't hurt anyone
3. Do what you want
4. Don't talk about fight club :3
Boxedfox.org
User avatar
Konomi
Obey the ears
 
Developer
Developer
 
Australia
Posts: 29
Joined:
Dec 2nd, '13, 02:45
Location:
Australia
Gender:
[♀] Female
Forum role:
Tech Guru
Operating system:
Linux

Return to General Development

 

Who is online

Users browsing this forum: No registered users and 1 guest

cron