Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6174845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:49:14+00:00 2026-05-23T23:49:14+00:00

How do i loop thru video files in a directory with extension .flv and

  • 0

How do i loop thru video files in a directory with extension .flv and use exec to convert each one of them (one at a time ) to mpg using ffmpeg -i original.avi final.mpg

While current file is converting, the next filename in the array should wait until exec process is complete before moving on and converting other filename in the array.

I know i can use scandir($dir) to make an array of all filenames, then maybe use split to only target .flv files. Is this the best way to do it. If i use a while loop to run thru each filename and do an exec on it, how do i make sure exec file is fully converted before moving on to the next file in the array.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T23:49:15+00:00Added an answer on May 23, 2026 at 11:49 pm
    $dir='/path/to/dir';
    $bash_commands = 'cd "'. $dir .'"
    for i in *.flv; do 
        OUTFILE=$(basename "$i" .flv).mpg
        ffmpeg -i "$i" -f mpg "$OUTFILE"
    done';
    exec($bash_commands);
    

    BASH code explanation:

    1. The first line changes the current working directory with the command cd.
      Refer: Moving around the filesystem @ tldp.org
    2. The second line starts a for loop that repeats the code inside it, while the $i variable contains the name of a file from the current working directory that is of the following pattern: *.flv. Each iteration changes the value of $i to the next file name in the current working directory.
      Refer: The for loop @ tldp.org and Loops @ tldp.org.
    3. The third line assigns the value $(basename "$i" .flv).mpg to the variable named OUTFILE.
      Refer: Subshells @ tldp.org, basename @ tlpd.org and Command Substitution @ tlpd.org.
    4. The fourth line initiates ffmpeg and tells it to convert the file in $i to a file named like $i, but with it’s suffix changed to “mpg” instead of “flv”. (The “suffix change” is done in the third line)
    5. The fifth line ends the for loop.

    The following should be the PHP equivalent:

    chdir('/path/to/current/working/dir');
    foreach (glob("*.flv") as $filename) {
        $OUTFILE = substr($filename, 0, -3) . 'mpg';
        exec('ffmpeg -i "'.$filename.'" -f mpg "'.$OUTFILE.'"');
    }
    

    Refer: glob(), chdir() and substr().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How would one loop thru a set of nodes, where the node name has
How can i loop thru a stl::List and store the value of one of
I have a loop that reads each line in a file using getline() :
How do i loop thru all dropdown boxes in JQuery?
What is the fastest way to loop thru a Query in T-SQL . 1)
Loop unwinding is a common way to help the compiler to optimize performance. I
The loop below is callign a proc that does various 'things' If it should
I want to loop over the contents of a text file and do a
How do I loop into all the resources in the resourcemanager? Ie: foreach (string
I want to loop over the elements of an HTML form, and store the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.