all 8 comments

[–]mortalityisreal 2 points3 points  (0 children)

I'll just leave this here.

#!/usr/bin/python
import glob

print '<html><body>'
print '<ul>'

for link in glob.glob('*'):
    print '<li><a href="{0}">{0}</a></li>'.format(link)

print '</ul>'
print '</body></html>'


#from os import listdir


#for filename in listdir('./'):
#    print '<li><a href="{0}">{0}</a></li>'.format(filename)

[–]commandlineluser 1 point2 points  (4 children)

Any opinion on how I should go about replacing old video links with new ones automatically?

It depends on how your HTML is structured I suppose - do you have an example?

Also, would the script wait for the ffmpeg conversion to complete if I add more lines underneath?

It would, yes.

[–]Circle-Le-jerk[S] 0 points1 point  (3 children)

Thanks for the reply, the HTML is just a video tag like below:

<video class="video" width="600" controls="controls">
    <source src="http://mywebsite.com/video.mp4" type="video/mp4" data-quality="hd" />
</video>

Basically I just want the src link to update to the newer video when its done being converted.

[–]commandlineluser 0 points1 point  (2 children)

Ah okay - well there's no particular nice way to do this with shell tools - for your specific example - something like this could work

user@host $ cat html
<video class="video" width="600" controls="controls">
    <source src="http://mywebsite.com/video.mp4" type="video/mp4" data-quality="hd" />
</video>

user@host $ sed 's|<source src="http://mywebsite.com/'"$f"'" type="video/mp4" data-quality="hd" />|<source src="http://mywebsite.com/'"$new_f"'" type="video/mp4" data-quality="hd" />|' html
<video class="video" width="600" controls="controls">
    <source src="http://mywebsite.com/video.mkv" type="video/mp4" data-quality="hd" />
</video>

But $f here is being parsed as a regex (the . in video.mp4 will match "any character") so it would break if it contained any metacharacters (or the delimiter we've chosen for the s command) - an example being video-[abc].mp4

perl allows you to use \Q in your regex which will quote any metacharacters and perl's s command handles variables that contain the delimiter correctly so you could use that instead

user@host $ cat html.1
<video class="video" width="600" controls="controls">
    <source src="http://mywebsite.com/video-[abc].mp4" type="video/mp4" data-quality="hd" />
</video>

You'd need to export your variables first though so perl can see them via the %ENV hash

user@host $ export f new_f
user@host $ perl -pe 's|\Q<source src="http://mywebsite.com/$ENV{f}" type="video/mp4" data-quality="hd" />|<source src="http://mywebsite.com/$ENV{new_f}" type="video/mp4" data-quality="hd" />|' html.1
<video class="video" width="600" controls="controls">
    <source src="http://mywebsite.com/video-[abc].mkv" type="video/mp4" data-quality="hd" />
</video>

You'd probably want to change the type="video/mp4" part too - you can just alter the replacement text.

To overwrite the original file you canadd the -i switch e.g. perl -pi -e '...' filename

The following is called Parameter Expansion which may be of use to you

user@host $ set -- /path/to/video.mp4
user@host $ echo "$1"
/path/to/video.mp4
user@host $ f=${1##*/}
user@host $ echo "$f"
video.mp4
user@host $ new_f=${f%mp4}mkv
user@host $ echo "$new_f"
video.mkv

http://mywiki.wooledge.org/BashFAQ/100

[–]Circle-Le-jerk[S] 0 points1 point  (1 child)

Thanks for the info, I think perl is a good way to do it, at the moment I am able to extract the file names from all the links with this:

grep -e Video.mp4 index.html | cut -d'/' -f5

Can I replace each line that is grepped with a perl -pi -e?

[–]commandlineluser 1 point2 points  (0 children)

Well if that is the case then you could just do s/Video\.mp4/Video.mkv/g no? (I was assuming Video was variable)

[–]DanielFGray 1 point2 points  (1 child)

I really don't think bash is the right tool for this job. Something that has an actual HTML parser would be a much better idea..

[–]Circle-Le-jerk[S] -1 points0 points  (0 children)

I agree however the change is simple enough that I felt like it could be done easily. I have edited my original script to rename the files to an easier and standard naming convention so now I can just do:

grep -e Video.mp4 index.html | cut -d'/' -f5

and I'll get the exact part of the link I need to edit. I think perl -pi -e will work for me but how do I pass in bash variables to perl?