DaemonForums  

Go Back   DaemonForums > Miscellaneous > Guides

Guides All Guides and HOWTO's.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 19th September 2022
bsd-keith bsd-keith is offline
Real Name: Keith
Open Source Software user
 
Join Date: Jun 2014
Location: Surrey/Hants Border, England
Posts: 344
Default Convert mp4 to mp3

Convert mp4 to mp3

I have a load of mp4 files with spaces in their names, the first line replaces the spaces with _ so that the second line can convert them to mp3.

Code:
#!/bin/sh

for f in *; do mv "$f" `echo $f | tr ' ' '_'`; done

for X in *.mp4;do ffmpeg -i $X $X.mp3;done
__________________
Linux since 1999, & also a BSD user.
Reply With Quote
  #2   (View Single Post)  
Old 19th September 2022
J65nko J65nko is offline
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 4,128
Default

Instead of using back ticks ```` (the key under the ~) you can use the POSIX command substitution"$( command )" construct. It is easier on the eyes, and does not have some quirks that plagues the older ` command ` format.
Code:
for f in *; do mv "$f" $( echo $f | tr ' ' '_'); done
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump

Last edited by J65nko; 19th September 2022 at 10:27 PM.
Reply With Quote
  #3   (View Single Post)  
Old 20th September 2022
bsd-keith bsd-keith is offline
Real Name: Keith
Open Source Software user
 
Join Date: Jun 2014
Location: Surrey/Hants Border, England
Posts: 344
Default

Thanks, it's the first time I've used tr, so copied what I'd found, (after trying it out in safety), & it worked OK. But I will alter my personal script to use $() in future.
__________________
Linux since 1999, & also a BSD user.
Reply With Quote
  #4   (View Single Post)  
Old 21st September 2022
victorvas victorvas is offline
Real Name: Victor
Linux
 
Join Date: May 2019
Posts: 148
Default

I think you need to use -vn flag to extract just the audio stream out of .mp4
Code:
ffmpeg -i video.mp4 -vn audio.mp3
Reply With Quote
  #5   (View Single Post)  
Old 21st September 2022
IdOp's Avatar
IdOp IdOp is offline
Too dumb for a smartphone
 
Join Date: May 2008
Location: twisting on the daemon's fork(2)
Posts: 1,027
Default

Also, if $X is "video.mp4" then isn't $X.mp3 going to be "video.mp4.mp3" ? Probably you want to get rid of the ".mp4" part of the final result? One way might be using

$( dirname $X )/$(basename $X .mp4).mp3

as the output file.
Reply With Quote
  #6   (View Single Post)  
Old 21st September 2022
bsd-keith bsd-keith is offline
Real Name: Keith
Open Source Software user
 
Join Date: Jun 2014
Location: Surrey/Hants Border, England
Posts: 344
Default

Yes, I had to use sed to swap '.mp4.' to just a single '.' - messy, but worked.

Code:
##To remove .mp4 from *.mp4.mp3 files
for file in *; do mv "$file" $(echo "$file" | sed -e 's/.mp4././g'); done

I also had to remove excess punctuation from filenames, this is what I used, (I expect there are better solutions, but again, it worked reasonably well).

Code:
##To keep A-Za-z0-9.- & remove all other
#!/bin/bash
for file in *; do mv "$file" $(echo "$file" | sed -e 's/[^A-Za-z0-9.-]//g'); done

As you can probably see, I'm no expert at scripting, but I noticed others were trying to do what I wanted to do, so posted what worked in my case.
__________________
Linux since 1999, & also a BSD user.
Reply With Quote
  #7   (View Single Post)  
Old 22nd September 2022
victorvas victorvas is offline
Real Name: Victor
Linux
 
Join Date: May 2019
Posts: 148
Default

Code:
for X in *.mp4; do ffmpeg -i $X ${X%.mp4}.mp3; done
This will get rid of .mp4 extension.
Reply With Quote
  #8   (View Single Post)  
Old 22nd September 2022
Head_on_a_Stick's Avatar
Head_on_a_Stick Head_on_a_Stick is offline
Real Name: Matthew
Bitchy Nerd Elitist
 
Join Date: Dec 2015
Location: London
Posts: 461
Default

And if the variables are properly quoted then the spaces don't have to be replaced with underscores:
Code:
for X in *.mp4; do ffmpeg -i "$X" "${X%.mp4}.mp3"; done
Reply With Quote
  #9   (View Single Post)  
Old 23rd September 2022
IdOp's Avatar
IdOp IdOp is offline
Too dumb for a smartphone
 
Join Date: May 2008
Location: twisting on the daemon's fork(2)
Posts: 1,027
Default

Quote:
Originally Posted by victorvas View Post
${X%.mp4}
Good find, thanks!
Reply With Quote
Old 23rd September 2022
victorvas victorvas is offline
Real Name: Victor
Linux
 
Join Date: May 2019
Posts: 148
Default

Quote:
Originally Posted by IdOp View Post
Good find, thanks!
You're welcome! There are others:
Code:
$ foo=some.thing.mp4

$ echo ${foo%.*}       
some.thing

$ echo ${foo%%.*}
some

$ echo ${foo#*m}
e.thing.mp4

$ echo ${foo##*m}
p4
Reply With Quote
Old 23rd September 2022
J65nko J65nko is offline
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 4,128
Default

These are documented in ksh(1):
Code:
The following forms of parameter substitution can also be used:

     [snip]

     ${name#pattern}
     ${name##pattern}
             If pattern matches the beginning of the value of parameter name,
             the matched text is deleted from the result of substitution.  A
             single `#' results in the shortest match, and two of them result
             in the longest match.

     ${name%pattern}
     ${name%%pattern}
             Like ${..#..} substitution, but it deletes from the end of the
             value.
From sh(1):
Code:
     ${parameter%[word]}
             Substitute parameter, deleting the smallest possible suffix
             matching word.

     ${parameter%%[word]}
             Substitute parameter, deleting the largest possible suffix
             matching word.

     ${parameter#[word]}
             Substitute parameter, deleting the smallest possible prefix
             matching word.

     ${parameter##[word]}
             Substitute parameter, deleting the largest possible prefix
             matching word.
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump
Reply With Quote
Old 23rd September 2022
Head_on_a_Stick's Avatar
Head_on_a_Stick Head_on_a_Stick is offline
Real Name: Matthew
Bitchy Nerd Elitist
 
Join Date: Dec 2015
Location: London
Posts: 461
Default

Keith's using a /bin/sh shebang so the POSIX reference is probably better:

https://pubs.opengroup.org/onlinepub...V3_chap02.html

(Section 2.6.2 — Parameter Expansion)
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
convmv - convert file names victorvas OpenBSD Packages and Ports 2 30th July 2022 03:05 AM
New Convert NFSv3 NFSv4 bigearsbilly OpenBSD General 1 6th March 2020 12:37 PM
sh script to convert inches to mm and cm J65nko Programming 6 8th August 2019 11:33 PM
Problem with convert aleunix OpenBSD Packages and Ports 2 10th May 2012 01:52 PM
Want to convert my server to raid revzalot OpenBSD Installation and Upgrading 2 16th September 2009 07:56 PM


All times are GMT. The time now is 07:23 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick