0

I have written this code to check for video files in a directory and convert them with ffmpeg. while running this script i get out put as "[ERROR] File Not Found" which i have it in else block.what is wrong with the script that i don't get the files.

#!/bin/bash
# set PATH to check existance of video file in this directory
checkfiles='/home/webuser/public_html/shareportal/convertedUp_videos/'
#format of output video file
webm='webm'
for f in checkfiles
do
fullfilename="$f"
filenamewithpath=$(basename "$fullfilename")
filewithoutext="${filename%.*}"
fileextention="${filename##*.}"
changeextension=mv --"$f" "${f%.$fileextention}.$webm"
outputfilename="/home/webuser/public_html/shareportal/converted_videos/$changeextension"
echo "File FUll NAME : $fullfilename"
echo "File name with full path : $filenamewithpath"
echo "File name without extention : $filewithoutext"
echo "File extention : $fileextention"
echo '1 File Converted'
if (ffmpeg -i "$f" "$outputfilename")
then
confullfilename="$outputfilename"
confilenamewithpath=$(basename "$confullfilename")
confilewithoutext="${filename%.*}"
confileextention="${filename##*.}"
echo "File FUll NAME : $confullfilename"
echo "File name with full path : $confilenamewithpath"
echo "File name without extention : $confilewithoutext"
echo "File extention : $confileextention"
echo '1 File Converted'
else
echo "Could Not Convert File"
fi
#get Image of video file on provided time stamp
image_path='/home/webuser/public_html/shareportal/video_images/$filewithoutext.png'
if (ffmpeg -ss 00:00:03 -i "$f" -vframes:v 1 "$image_path")
then
echo "Image Extracted"
else
echo "Could not Extract Image"
fi
done
rm -r f
A Sahra
  • 118
  • 1
  • 12
  • `if [ -f "$checkfiles" ]` that isn't right. `checkfiles` is a *directory*. `-f` checks for a regular file. – kaylum Dec 03 '16 at 06:12
  • 1
    So what is the difference between a regular file and a file in a directory? – A Sahra Dec 03 '16 at 06:14
  • 1
    It doesn't check for files in a directory. `-f` checks whether that exact path is a file or not. A directory is not a regular file. You need to have the check inside a for loop which iterates through a list of files and checks each file. – kaylum Dec 03 '16 at 06:17
  • 1
    Possible duplicate of [How to iterate over files in directory with bash?](http://stackoverflow.com/questions/20796200/how-to-iterate-over-files-in-directory-with-bash) – kaylum Dec 03 '16 at 06:18
  • 1
    The duplicate you suggested : the extension of files is .txt which we can set it statically but here the extension varies from one video format to other do i need to check for extension first? – A Sahra Dec 03 '16 at 06:29
  • 1
    Of course you need to apply the answer to your situation. Instead of `*.txt` you can just use `*` to get all the files. – kaylum Dec 03 '16 at 06:30
  • 1
    @kaylum is it right the way i have checked the path for files i have updated the code a bit which i have removed if statement and i directly move to iteration part? – A Sahra Dec 04 '16 at 05:04
  • 2
    No it is not correct. The bash `for` loop iterates over the list of items given. It does not know about directories or files. You can't just give a directory to a `for` loop and expect it to loop over the files. That is just not how it works. As the link shows, you need to do `for f in ${checkfiles}*` where `${checkfiles}*` will expand to list all the files in that directory (just like `ls *`) – kaylum Dec 04 '16 at 05:36
  • 1
    @kaylum Thanks man that worked really well – A Sahra Dec 04 '16 at 10:20

1 Answers1

0

How to iterate over files in directory with bash?

The bash for loop iterates over the list of items given. It does not know about directories or files. You can't just give a directory to a for loop and expect it to loop over the files. That is just not how it works. As the link shows, you need to do for f in ${checkfiles} where ${checkfiles} will expand to list all the files in that directory (just like ls *) – kaylum

Armali
  • 18,255
  • 14
  • 57
  • 171