Jump to content

Example on how to work with several files needed


Recommended Posts

I want do do a simple processing of several files with bash/Alfred file action. I am a bit embarrassed to ask, but I just don't get how to process several files. I read, Alfred passes the selected files to the workflow and I should use argv. But I searched the Forum and the web for hours, I found no example.

My script is easy as this: 

ffmpeg -i sys.argv[1] -vcodec copy sys.argv[1].mp4

But apparently, this is not how it works. The Debugger says "no matches found: sys.argv[1]" 

 

What does work, but only for single files:

ffmpeg -i {query} -vcodec copy {query}.mp4

When I run this for several files, Terminal asks me if I want to overwrite the existing file before doing anything else.

 

What am I missing? I know it is easy, but I could just not find an answer… Any hint appreciated!

Link to comment
3 hours ago, jjaco said:

But apparently, this is not how it works.

 

Indeed not. Your script is nonsense. It's a mish-mash of shell script and Python.

 

3 hours ago, jjaco said:

I just don't get how to process several files

 

That depends on whether you want to process them one at a time or all at once.

 

Tell us what you're actually trying to achieve, not the ways you've failed to do it.

Link to comment

I just want to execute the command above:

ffmpeg -i {query} -vcodec copy {query}.mp4

for all the files selected in Finder. I don't care if this happens one after the other or all together. Might be better if one after the other, so the machine is not blocked when processing too many at once.

Link to comment
1 hour ago, jjaco said:

 

I just want to execute the command above

 


That is not what you're trying to achieve. That is how you're trying to do it. And we already know that it doesn't work.

 

What are you actually trying to achieve? Remove the audio from a bunch of video files?

 

 

Link to comment

I want to learn how to use the Workflow feature. And I want to understand how to do something with several files, not only one.

And my example project is making mp4 files from flv files without recoding. That is what I did with ffmpeg via Terminal before. 

Link to comment
7 hours ago, jjaco said:

And I want to understand how to do something with several files, not only one.


Sure, but I need to know if you want to do something to each of the files or all of them together.

 

If you want to process each file in a sequence, you need to use a loop. Something like:

 

for f in $@; do

  ffmpeg -i "$f" -vcodec copy "${f}.mp4"

done

 

Note: I haven't tested this: I'm on my phone at work. It should be close to correct, though.

Link to comment

Thanks, sounds good.

I made some mistake, though. Debugger says:

[12:00:14.053] Convert flv to mp4[File Action] Processing complete
[12:00:14.058] Convert flv to mp4[File Action] Passing output '(
    "/Users/me/Dropbox/Strassenszenen_Frankfurt_20er_Jahre.flv",
    "/Users/me/Dropbox/Paul.flv"
)' to Run Script
[12:00:14.077] ERROR: Convert flv to mp4[Run Script] /Users/me/Library/Caches/com.runningwithcrayons.Alfred/Workflow Scripts/0B1A8350-B332-46E9-966D-284D2305D6A1: line 2: ffmpeg: command not found
/Users/me/Library/Caches/com.runningwithcrayons.Alfred/Workflow Scripts/0B1A8350-B332-46E9-966D-284D2305D6A1: line 2: ffmpeg: command not found
/Users/me/Library/Caches/com.runningwithcrayons.Alfred/Workflow Scripts/0B1A8350-B332-46E9-966D-284D2305D6A1: line 2: ffmpeg: command not found
/Users/me/Library/Caches/com.runningwithcrayons.Alfred/Workflow Scripts/0B1A8350-B332-46E9-966D-284D2305D6A1: line 2: ffmpeg: command not found
/Users/me/Library/Caches/com.runningwithcrayons.Alfred/Workflow Scripts/0B1A8350-B332-46E9-966D-284D2305D6A1: line 2: ffmpeg: command not found
/Users/me/Library/Caches/com.runningwithcrayons.Alfred/Workflow Scripts/0B1A8350-B332-46E9-966D-284D2305D6A1: line 2: ffmpeg: command not found
/Users/me/Library/Caches/com.runningwithcrayons.Alfred/Workflow Scripts/0B1A8350-B332-46E9-966D-284D2305D6A1: line 2: ffmpeg: command not found
/Users/me/Library/Caches/com.runningwithcrayons.Alfred/Workflow Scripts/0B1A8350-B332-46E9-966D-284D2305D6A1: line 2: ffmpeg: command not found

I copied your script, the settings are in the image attached.

 

Any ideas?SettingsAlfred.thumb.png.0be1dc1a497bf6fbeb52e97322f36722.png

 

Link to comment

Ah, cool, almost there!
Now it works as long as I don't have any spaces in the filenames. Somehow the escaping seems not to work. The bug report is:

[12:13:33.077] ERROR: Convert flv to mp4[Run Script] ffmpeg version 3.1.1 Copyright (c) 2000-2016 the FFmpeg developers
  built with Apple LLVM version 7.3.0 (clang-703.0.31)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/3.1.1 --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-opencl --enable-libx264 --enable-libmp3lame --enable-libxvid --disable-lzma --enable-vda
  libavutil      55. 28.100 / 55. 28.100
  libavcodec     57. 48.101 / 57. 48.101
  libavformat    57. 41.100 / 57. 41.100
  libavdevice    57.  0.101 / 57.  0.101
  libavfilter     6. 47.100 /  6. 47.100
  libavresample   3.  0.  0 /  3.  0.  0
  libswscale      4.  1.100 /  4.  1.100
  libswresample   2.  1.100 /  2.  1.100
  libpostproc    54.  0.100 / 54.  0.100
/Users/me/Downloads/bsp: No such file or directory
ffmpeg version 3.1.1 Copyright (c) 2000-2016 the FFmpeg developers
  built with Apple LLVM version 7.3.0 (clang-703.0.31)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/3.1.1 --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-opencl --enable-libx264 --enable-libmp3lame --enable-libxvid --disable-lzma --enable-vda
  libavutil      55. 28.100 / 55. 28.100
  libavcodec     57. 48.101 / 57. 48.101
  libavformat    57. 41.100 / 57. 41.100
  libavdevice    57.  0.101 / 57.  0.101
  libavfilter     6. 47.100 /  6. 47.100
  libavresample   3.  0.  0 /  3.  0.  0
  libswscale      4.  1.100 /  4.  1.100
  libswresample   2.  1.100 /  2.  1.100
  libpostproc    54.  0.100 / 54.  0.100
copy.flv: No such file or directory

The filename is "bsp copy.flv". If I use "bsp_copy.flv", it works.

 

Since I don't understand yet how the "$f" and "${f}.mp4" work, I am still stuck… any hint appreciated!

Link to comment

Yay, this works!! Thanks a ton.

 

Could you point me to an explanation why working with the input as argv works? I now have this workflow, which is great. But when I want to create new ones, I can't ask you every time for your help 😉

In particular, why do I write $f in the first instance and ${f} in the second?

Link to comment
2 hours ago, jjaco said:

an explanation why working with the input as argv works


ARGV is an array containing multiple arguments. {query} is only a single argument. If you want the script to handle multiple files, then you need to use the input method that supports multiple arguments.

 

2 hours ago, jjaco said:

why do I write $f in the first instance and ${f} in the second?


They mean the same thing. ${f} explicitly delimits the variable name, so ${f}01 is the variable named "f" plus the string "01", while $f01 is variable named "f01".

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...