LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 04-26-2013, 06:36 PM   #1
fakie_flip
Senior Member
 
Registered: Feb 2005
Location: san antonio, texas
Distribution: Fedora 64 bit RAID0 + LUKS, CentOS (server), Backtrack, Gentoo Hardened
Posts: 1,440

Rep: Reputation: 80
Quotes around variable not helping with spaces in file names


I am trying to batch convert a bunch of .mov apple quicktime videos, so that when I transfer them over to my Android tablet, Android can play them. The file names have spaces in them, so I used quotes, "$x".

Code:
find . -iname '*mov' -exec ffmpeg -i "$x" copy -acodec copy "$x.mp4" \;
However I am still getting errors like this:

Quote:
00.03. Using the exercise files.mov: No such file or director
 
Old 04-26-2013, 08:20 PM   #2
Habitual
Senior Member
 
Registered: Jan 2011
Distribution: Slack14_64_Multilib
Posts: 3,070
Blog Entries: 4

Rep: Reputation: 741Reputation: 741Reputation: 741Reputation: 741Reputation: 741Reputation: 741Reputation: 741
http://mywiki.wooledge.org/BashPitfalls may help?
I could be 'off'
 
Old 04-26-2013, 09:39 PM   #3
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 1,364

Rep: Reputation: 586Reputation: 586Reputation: 586Reputation: 586Reputation: 586Reputation: 586
Quote:
Originally Posted by fakie_flip View Post
Code:
find . -iname '*mov' -exec ffmpeg -i "$x" copy -acodec copy "$x.mp4" \;
What on earth do you think "$x" is in that construct? The find command uses a pair of curly braces to indicate where the current filename should be substituted in the command:
Code:
find . -iname '*mov' -exec ffmpeg -i {} copy -acodec copy {}.mp4 \;
That won't quite do what you probably want, since you will end up with files named "xxxxxx.mov.mp4". Getting rid of that extraneous ".mov" needs an extra step:
Code:
find . -iname '*mov' -exec ffmpeg -i {} copy -acodec copy {}.mp4 \; -exec rename .mov.mp4 .mp4 {}.mp4 \;
Best I could come up with quickly (and it won't remove ".MOV", only ".mov"). Perhaps there's a cleaner way to do that.

Last edited by rknichols; 04-26-2013 at 09:41 PM.
 
1 members found this post helpful.
Old 04-27-2013, 03:39 AM   #4
fakie_flip
Senior Member
 
Registered: Feb 2005
Location: san antonio, texas
Distribution: Fedora 64 bit RAID0 + LUKS, CentOS (server), Backtrack, Gentoo Hardened
Posts: 1,440

Original Poster
Rep: Reputation: 80
Quote:
Originally Posted by rknichols View Post
What on earth do you think "$x" is in that construct? The find command uses a pair of curly braces to indicate where the current filename should be substituted in the command:[code]
I was too tired and before that, I was using a for loop with x instead of find. My mistake.

Quote:
find . -iname '*mov' -exec ffmpeg -i {} copy -acodec copy {}.mp4 \; -exec rename .mov.mp4 .mp4 {}.mp4 \;
I hadn't realized -exec could be called more than once in a single find command, good to know this can be done! Thanks.

Last edited by fakie_flip; 04-27-2013 at 05:05 AM.
 
Old 04-27-2013, 06:04 AM   #5
fakie_flip
Senior Member
 
Registered: Feb 2005
Location: san antonio, texas
Distribution: Fedora 64 bit RAID0 + LUKS, CentOS (server), Backtrack, Gentoo Hardened
Posts: 1,440

Original Poster
Rep: Reputation: 80
Here's the command I put together and ended up using.

Code:
find . -iname '*mov' -exec ffmpeg -i '{}' -vcodec copy -acodec copy '{}.mp4' \; -exec rename 's/\.mov.mp4$/\.mp4/' '{}.mp4' \; -exec rm -vf '{}' \;

Last edited by fakie_flip; 04-27-2013 at 06:16 AM.
 
Old 04-29-2013, 10:33 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
Since your commands are getting rather complex, I'd substitute a shell loop instead of -exec. Just be sure to use null separators for the input file list.

Code:
while IFS='' read -rd '' fname; do

    ffmpeg -i "$fname" -vcodec copy -acodec copy "${fname%.*}.mp4"

    if (( $? )); then
        echo "something went wrong processing file: $fname."
    else
        rm -fv "$fname"
    fi

done < <( find . -iname '*mov' -print0 )
A similar option would be to put your commands in a separate script, configured so that it will loop over multiple input files, then run that with find.

Code:
#!/bin/bash
# convertscript.sh -- converts the input files to mp4
# and removes the originals is successful

for fname ; do

    ffmpeg -i "$fname" -vcodec copy -acodec copy "${fname%.*}.mp4"

    if (( $? )); then
        echo "something went wrong processing file: $fname."
    else
        rm -fv "$fname"
    fi

done

exit 0
Then run it like this:

Code:
find . -iname '*mov' -exec /path/to/convertscript.sh '{}' +
The main advantage here is that you can now use the '+' option to -exec. this processes the arguments in batch mode, saving you process overhead. The only limitation is that '{}' now can only be included once, at the end of the command, and that command must be able to accept a list of files as arguments.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] File names with spaces in them. stf92 Programming 9 04-22-2012 11:29 AM
replacing single quotes in file names adenardo Linux - General 2 05-27-2005 11:29 AM
how to handle file names with spaces in them bahadur Programming 3 03-23-2005 05:15 AM
du or wc and file names with spaces bramadams Slackware 2 01-27-2005 11:43 AM
Spaces in file names JohnKFT Slackware 3 11-09-2004 03:44 PM


All times are GMT -5. The time now is 01:32 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration